Fetching a line between two lines using a bash shell script

I saw similar questions, but none of the solutions seem to work in this case. I have a text file that looks something like this.

START-OF-FILE
RUNDATE=20140910
FIRMNAME=dl
FILETYPE=pc
REPLYFILENAME=TEST
DERIVED=yes
PROGRAMFLAG=oneshot
SECID=ISIN
SECMASTER=yes
PROGRAMNAME=getdata
START-OF-FIELDS
ISSUER
START-OF-DATA
US345370CN85|0|4|FORD MOTOR COMPANY|FORD MOTOR COMPANY| | |
US31679BAC46|0|4|FIFTH STREET FINANCE COR|FIFTH STREET FINANCE COR| | |
END-OF-DATA
END-OF-FILE

I am trying to write a bash shell to extract only text between "START DATA" and "END DATA", with the exception of both. So the output I'm looking for will look like this:

US345370CN85|0|4|FORD MOTOR COMPANY|FORD MOTOR COMPANY| | |
US31679BAC46|0|4|FIFTH STREET FINANCE COR|FIFTH STREET FINANCE COR| | |

The code I have written so far is as follows

while read line
do
    name=$line

    echo $name | sed -e 's/START-OF-DATA\(.*\)END-OF-DATA/\1/'

done < $1

and run it from bash e.g.

./script.sh file.txt

script.sh - , script as, file.txt - , . . , - . .

+4
3

awk, :

awk '/START-OF-DATA/{p=1;next} /END-OF-DATA/{p=0;exit} p' file
US345370CN85|0|4|FORD MOTOR COMPANY|FORD MOTOR COMPANY| | |
US31679BAC46|0|4|FIFTH STREET FINANCE COR|FIFTH STREET FINANCE COR| | |

sed:

sed -n '/START-OF-DATA/,/END-OF-DATA/{/START-OF-DATA\|END-OF-DATA/!p;}' file
US345370CN85|0|4|FORD MOTOR COMPANY|FORD MOTOR COMPANY| | |
US31679BAC46|0|4|FIFTH STREET FINANCE COR|FIFTH STREET FINANCE COR| | |
+5

, , "START-OF-DATA", "True" ( ), , "END-OF-DATA". , -, "True" ( ).

... sed:

sed -n '/START-OF-DATA/,/END-OF-DATA/ { //!p }' file.txt
+2

perlish grep, :

grep -Pzo "(?s)START-OF-DATA.*END-OF-DATA" "$1"

START-OF-DATA END-OF-DATA. , :

grep -Pzo "(?s)(?<=START-OF-DATA\n).*(?=\nEND-OF-DATA)"

(?<=START-OF-DATA\n) (?=\nEND-OF-DATA) , perlre, , .

+1

All Articles