How to print data between certain tags?
You can use this sed
:
sed -n '/^000|/,/^010|/{/^0[01]0|/!p;}' file102|000|DDKSB=DAGA;DAGA=ADGA;DAG-FGSA=ADGA|0001|KLJDFLKJBDL|00|ADGAHA||00|ASYAHA|||DAGHAH|0|GAFDGA|18||3|N|1||AHA|ASGAN|ASFAN||82|1||2|300|||0|0|0|0|10|0||0|0|KLJDFLKJBDL|2||||||||102|0100|DDKSB=DAGA;DAGA=ADGA;DAG-FGSA=ADGA|00|KLJDFLKJBDL|00|ASDGAHA||00|ASYAHA|||DAGHAH|0|AGAH|5||3|N|1||AHA|ASGAN|ASDHAH||82|1||2|300|||0|0|0|0|54|0||0|0|KLJDFLKJBDL|2||||||||
Using in a find
command:
find . -name '*.txt' -exec sed -i '' -n '/^000|/,/^010|/{/^0[01]0|/!p;}' {} \;
you can try,
awk -v FS="|" '$1=="000",$1=="010" {print > "output.txt"}' input_file.txt
you get,
000|FILE___V20170307-003792102|000|DDKSB=DAGA;DAGA=ADGA;DAG-FGSA=ADGA|0001|KLJDFLKJBDL|00|ADGAHA||00|ASYAHA|||DAGHAH|0|GAFDGA|18||3|N|1||AHA|ASGAN|ASFAN||82|1||2|300|||0|0|0|0|10|0||0|0|KLJDFLKJBDL|2||||||||102|0100|DDKSB=DAGA;DAGA=ADGA;DAG-FGSA=ADGA|00|KLJDFLKJBDL|00|ASDGAHA||00|ASYAHA|||DAGHAH|0|AGAH|5||3|N|1||AHA|ASGAN|ASDHAH||82|1||2|300|||0|0|0|0|54|0||0|0|KLJDFLKJBDL|2||||||||010|ENDOFFILE|10
also only rows between the first row and the last row
awk -v FS="|" '$1=="010"{f=0} f{print > "output.txt"} $1=="000"{f=1}' input_file.txt
you get,
102|000|DDKSB=DAGA;DAGA=ADGA;DAG-FGSA=ADGA|0001|KLJDFLKJBDL|00|ADGAHA||00|ASYAHA|||DAGHAH|0|GAFDGA|18||3|N|1||AHA|ASGAN|ASFAN||82|1||2|300|||0|0|0|0|10|0||0|0|KLJDFLKJBDL|2||||||||102|0100|DDKSB=DAGA;DAGA=ADGA;DAG-FGSA=ADGA|00|KLJDFLKJBDL|00|ASDGAHA||00|ASYAHA|||DAGHAH|0|AGAH|5||3|N|1||AHA|ASGAN|ASDHAH||82|1||2|300|||0|0|0|0|54|0||0|0|KLJDFLKJBDL|2||||||||
To get rows between the first row and the last row not considering the content at all, using awk:
$ awk 'NR>2{print p} {p=$0}' file102|000|DDKSB=DAGA;DAGA=ADGA;DAG-FGSA=ADGA|0001|KLJDFLKJBDL|00|ADGAHA||00|ASYAHA|||DAGHAH|0|GAFDGA|18||3|N|1||AHA|ASGAN|ASFAN||82|1||2|300|||0|0|0|0|10|0||0|0|KLJDFLKJBDL|2||||||||102|0100|DDKSB=DAGA;DAGA=ADGA;DAG-FGSA=ADGA|00|KLJDFLKJBDL|00|ASDGAHA||00|ASYAHA|||DAGHAH|0|AGAH|5||3|N|1||AHA|ASGAN|ASDHAH||82|1||2|300|||0|0|0|0|54|0||0|0|KLJDFLKJBDL|2||||||||
Using head
and tail
:
$ head -n -1 file |tail -n +2
man head
:
-n, --lines=[-]K print the first K lines instead of the first 10; with the leading '-', print all but the last K lines of each file
man tail
:
-n, --lines=K output the last K lines, instead of the last 10; or use -n +K to output lines starting with the Kth
If you have several files, you can:
for f in files* ; do head -n -1 "$f" |tail -n +2 > newpath/"$f" ; done