Zach
Zach

Reputation: 5

How to use grep,awk or sed to extract text between two strings?

Please help me in using either SED, AWK, or GREP to extract the following text. I have files that look similar to this.

Text text text text text text text
Text text text text text text text
   Table A
<TABLE>
xxx xxx xxx xxx
xxx xxx xxx xxx
</TABLE>
Text text text text text text text
Text text text text text text text
   Table B
<TABLE>
xxx xxx xxx xxx
xxx xxx xxx xxx
</TABLE>

I need all of the info for only table A but am not sure how to go about doing so.

Upvotes: 0

Views: 542

Answers (3)

Mark Mishyn
Mark Mishyn

Reputation: 4151

You even can you grep for that, but awk seems like better.

grep -A1000 "Table A" file.txt | grep -B1000 "Table B"

Upvotes: 0

Ed Morton
Ed Morton

Reputation: 204488

Either of these may be what you want, depending on what your expected output and the rest of your text looks like:

$ awk '/Table A/{f=1} f{print; if (/<\/TABLE>/) exit}' file
   Table A
<TABLE>
xxx xxx xxx xxx
xxx xxx xxx xxx
</TABLE>

$ awk 'f{print; if (/<\/TABLE>/) exit} /Table A/{f=1}' file
<TABLE>
xxx xxx xxx xxx
xxx xxx xxx xxx
</TABLE>

Upvotes: 1

Rob
Rob

Reputation: 2654

As long as there are no </TABLE> inside the TABLE element.

sed -n '/Table A/,/<\/TABLE>/p' | grep -v "Table A" 

PS: the grep -v is probably not necessary I just do not know off the top of my head the option to not include the starting pattern.

That will print out

 <TABLE>
  xxx xxx xxx xxx
  xxx xxx xxx xxx
 </TABLE>

If you need the Text part this will not work. AWK would probably be better.

If you need something with a variable name you can do

 myTableName="Table A"
 sed -n "/${myTableName}/,/<\/TABLE>/p" | grep -v ${myTableName}

Upvotes: 0

Related Questions