Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract lines between 2 tokens in a text file using bash

Tags:

i have a text file which looks like this:

random useless text  <!-- this is token 1 -->  para1  para2  para3  <!-- this is token 2 -->  random useless text again 

I want to extract the text in between the tokens (excluding the tokens of course). I tried using ## and %% to extract the data in between but it didn't work. I think it is not meant for manipulating such large text files. Any suggestions how i can do it ? maybe awk or sed ?

like image 260
tapan Avatar asked Jan 31 '11 23:01

tapan


People also ask

How do you put a space between two lines in a shell script?

The G command appends a newline and the hold space to the end of the pattern space. Since, by default, the hold space is empty, this has the effect of just adding an extra newline at the end of each line. The prefix $! tells sed to do this on all lines except the last one.

How do you print lines in Linux?

To print a document on the default printer, just use the lp command followed by the name of the file you want to print.

How do I find the common line of two files?

Use comm -12 file1 file2 to get common lines in both files. You may also needs your file to be sorted to comm to work as expected. Or using grep command you need to add -x option to match the whole line as a matching pattern. The F option is telling grep that match pattern as a string not a regex match.


1 Answers

No need for head and tail or grep or to read the file multiple times:

sed -n '/<!-- this is token 1 -->/{:a;n;/<!-- this is token 2 -->/b;p;ba}' inputfile 

Explanation:

  • -n - don't do an implicit print
  • /<!-- this is token 1 -->/{ - if the starting marker is found, then
    • :a - label "a"
      • n - read the next line
      • /<!-- this is token 2 -->/q - if it's the ending marker, quit
      • p - otherwise, print the line
    • ba - branch to label "a"
  • } end if
like image 174
Dennis Williamson Avatar answered Sep 27 '22 20:09

Dennis Williamson