LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-20-2017, 01:43 PM   #1
threezerous
Member
 
Registered: Jul 2009
Posts: 96

Rep: Reputation: 16
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
 
Old 01-20-2017, 02:21 PM   #2
Turbocapitalist
Senior Member
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 4,362
Blog Entries: 3

Rep: Reputation: 2176Reputation: 2176Reputation: 2176Reputation: 2176Reputation: 2176Reputation: 2176Reputation: 2176Reputation: 2176Reputation: 2176Reputation: 2176Reputation: 2176
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
 
Old 01-20-2017, 02:59 PM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,760

Rep: Reputation: 3050Reputation: 3050Reputation: 3050Reputation: 3050Reputation: 3050Reputation: 3050Reputation: 3050Reputation: 3050Reputation: 3050Reputation: 3050Reputation: 3050
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
 
Old 01-31-2017, 09:44 AM   #4
threezerous
Member
 
Registered: Jul 2009
Posts: 96

Original Poster
Rep: Reputation: 16
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
 
Old 01-31-2017, 11:03 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,760

Rep: Reputation: 3050Reputation: 3050Reputation: 3050Reputation: 3050Reputation: 3050Reputation: 3050Reputation: 3050Reputation: 3050Reputation: 3050Reputation: 3050Reputation: 3050
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
libre office writer table 4.2.7.2 build 2 keeps losing cell and table settings 1sweetwater! Linux - Software 5 12-10-2014 01:19 PM
why did I lose my table cell lines format when I changed the cell 1sweetwater! Linux - Software 1 12-03-2014 05:06 PM
Nautilus never returns any search results hongman Debian 6 10-31-2014 12:09 PM
Script: how to assign values to variables using "awk" results? JZL240I-U Linux - Software 3 11-18-2008 01:59 AM
Abiword --- Can't insert picture in table cell rickh Linux - Software 2 03-07-2006 01:53 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:36 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration