LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   awk script returns a table. need results of 3 rd cell (https://www.linuxquestions.org/questions/linux-newbie-8/awk-script-returns-a-table-need-results-of-3-rd-cell-4175597903/)

threezerous 01-20-2017 12:43 PM

awk script returns a table. need results of 3 rd cell
 
Using an awk script as below to search through a file,
Code:

awk '/Single-Item/{n++; if (n==3)f=3;} f{print;f--}'
I get a table output as below
Quote:

<td scope='row' align='right' class='fieldlabel' height='30'><span class=defaultLabelStyle>History</span></td>
<td class='defaultcolumnspacer'>&nbsp;:&nbsp;</td>
<td>6.423 mins</td>
I need the results of the third cell...."6.423 mins"

I am guessing another awk script to iterate through td string would be helpful, but is there any better way to do it?

Thanks in advance for reading and giving your valuable time

Turbocapitalist 01-20-2017 01:21 PM

What you have is not well-formed XML so most parsers will choke.

But, nonetheless, a parser is what you need for XML (or XHTML as the case may be) unless the input is flawlessly regular. If it is XHTML then you can use one of the many XHTML parsers as they tend to be a bit more tolerant. In that case the xpath you are looking for would be '(//td)[3]' or '//td[3]'

My approach would be to whip up a few lines of perl for that using HTML::TreeBuilder::XPath

grail 01-20-2017 01:59 PM

Above is the better plan, but if you want to keep what you have, simply look at your math. Currently you are saying once you find the third occurrence, starting printing the next 3 lines.
Just increase 'n' and only print 1 line. If you cannot guarantee the order of the data, then as advised, use a better tool :)

threezerous 01-31-2017 08:44 AM

Thanks for the tips and apologies for a late response. I was able to get the desired output with the following code
Code:

#!/bin/sh

str=awk '/Single-Item/{n++; if (n==3)f=3;} f{print;f--}' | grep '<td>' | sed -e 's/<\/*td>//g'
echo $str | grep secs


grail 01-31-2017 10:03 AM

Not sure if you checked what you have written, but I can guarantee that the code presented will never work.

1. str will equal the string 'awk' and the rest of the line will cause an error

2. Excluding the above error, the awk has no file to read from, so if it did work it would just hang

3. awk has all the power of grep and sed plus more, so not sure why you would use all 3

4. If you have to run an additional grep after all of the previous lines work, you need to rethink your process


All times are GMT -5. The time now is 10:14 AM.