0

I need to extract the text between two headers, if the first one matches with a source file for lookup the headers to search, example:

&Header1

1231241241313124123213123214124123213213124124123123212

1231231231231231231231231231232131242141241241231325552

2132141241232132132132141251232132142142132132132142412

&Header2

1231241241313124123213123214124123213213124124123123212

2132141241232132132132141251232132142142132132132142412

&Header3

1231241241313124123213123214124123213213124124123123212

1231231231231231231231231231232131242141241241231325552

213214124123213213213214125123213214

And my source file:

&Header1

&Header3

So only retrieve header1 and 3 with the number information bellow.

  • are these header exiting only once in the file or can there be multiple instances ? And in either case, will there be an unmatched header. Like, for instance, in the given example, would there be a &Header1 without a matching &Header3 or vice-versa? – MelBurslan May 18 '16 at 15:08
  • Those headers are unique in the big file, to be precisely I have 255k headers/labels – User_Random39 May 18 '16 at 15:10
  • If you are using this in a two pass fashion, pass 1 could split at the header lines, and make a file in a temporary directory. named for that header string, and containing the data for that header. During pass 2, your processor would treat a detected header call as 'get data in file defined by header'. Using the filesystem in a db sort of fashion – infixed May 18 '16 at 16:37
1
startheader=$(head -1 sourcefile)
endheader=$(tail -1 sourcefile)

# above lines assume your sourcefile has two lines in it and 
# each line contains the starting header and ending header

startlinenumber=$(grep -n "${startheader}" datafile|cut -d: -f1)
endlinenumber=$(grep -n "${endheader}" datafile|cut -d: -f1)

sed -n -e "${startlinenumber},${endlinenumber}p" datafile

I am pretty sure, there is a more elaborate way of doing this with either awk or perl or maybe a single liner sed command but I just wanted to give you the logic explicitly. You can play with it and make it fit to your needs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.