0

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.

Zach
  • 5
  • 2
  • 1
    what id the distinguishing part of `Table A`, what is the actual content? – RomanPerekhrest Jan 31 '17 at 18:58
  • 1
    replace all the text, text, text placeholders with actual, truly representative sample text. Add the expected output given that input. – Ed Morton Jan 31 '17 at 19:06
  • Possible duplicate of [Sed to extract text between two strings](https://stackoverflow.com/questions/16643288/sed-to-extract-text-between-two-strings) – tripleee Aug 22 '18 at 09:53

3 Answers3

1

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>
Ed Morton
  • 188,023
  • 17
  • 78
  • 185
0

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}
Rob
  • 2,618
  • 2
  • 22
  • 29
  • Don't do that. Ranges make trivial tasks very slightly briefer but then require duplicated conditions (as shown above) or complete rewrites given the tiniest requirements change. Just use a flag instead of a range (which means you should use awk, not sed and/or grep). – Ed Morton Jan 31 '17 at 19:08
0

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

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

Mark Mishyn
  • 3,921
  • 2
  • 28
  • 30