LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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 11-14-2007, 10:42 PM   #1
packets
Member
 
Registered: Oct 2005
Posts: 339

Rep: Reputation: 32
how grep show certain row only


I have mrtg and want to dump the current in/out to a text file.
Is there anyone knows how to grep certain row (like ROW3 and ROW4 only)?
awk can print certain column, is there a binary that can print certain rows? Like the ff, when I do "cat 202.84.22.161_5.html | egrep "kb/s|b/s" it shows the ff. How can I print ROW3 and ROW4 only?

Anyone suggest

<td>231.7 kb/s (9.7%)</td>
<td>25.0 kb/s (1.0%) </td>
<td>21.7 kb/s (0.9%) </td> --> ROW3
<td>180.1 kb/s (7.5%) </td>
<td>6160.0 b/s (0.3%) </td>
<td>7768.0 b/s (0.3%) </td> --> ROW 6
<td>241.8 kb/s (10.1%)</td>
<td>9952.0 b/s (0.4%) </td>
<td>24.1 kb/s (1.0%) </td>
<td>68.8 kb/s (2.9%) </td>
<td>2520.0 b/s (0.1%) </td>
<td>6848.0 b/s (0.3%) </td>
<td>183.2 kb/s (7.6%)</td>
<td>4608.0 b/s (0.2%) </td>
<td>23.7 kb/s (1.0%) </td>
<td>28.5 kb/s (1.2%) </td>
<td>1480.0 b/s (0.1%) </td>
<td>4384.0 b/s (0.2%) </td>
<td>28.2 kb/s (1.2%)</td>
<td>3432.0 b/s (0.1%) </td>
<td>14.4 kb/s (0.6%) </td>
<td>5288.0 b/s (0.2%) </td>
<td>1216.0 b/s (0.1%) </td>
<td>3552.0 b/s (0.1%) </td>
 
Old 11-14-2007, 10:50 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
awk and sed can ...


Code:
awk ' NR == 4 || NR == 3 {print }' file

Code:
sed -n '3,4p' file


Cheers,
Tink
 
Old 11-15-2007, 12:25 AM   #3
packets
Member
 
Registered: Oct 2005
Posts: 339

Original Poster
Rep: Reputation: 32
This one works

Quote:
awk ' NR == 4 || NR == 3 {print }'
This one didn't. I tried to show certain 3 and 6 but it print 4 lines.
The command I did is "cat test.txt | sed -n '3,6p'" and it print the ff:

<td>28.5 kb/s (1.2%) </td> --> ROW 3
<td>180.1 kb/s (7.5%) </td> --> ROW4
<td>6248.0 b/s (0.3%) </td>
<td>5936.0 b/s (0.2%) </td>


Quote:
sed -n '3,4p'
 
Old 11-15-2007, 12:33 AM   #4
packets
Member
 
Registered: Oct 2005
Posts: 339

Original Poster
Rep: Reputation: 32
I tried this:

cat test.txt | awk ' NR == 1 || NR == 9 {print }' # 1 and 9 are the column

<td>231.7 kb/s (9.7%)</td>
<td>16.8 kb/s (0.7%) </td>

It works. The limitation here is how to get rid of <td> and show only 231.7 kb/s and 16.8 kb/s?
If only there is a space after the > , there could be no problem since awk/cut can remove this but since not all has no space, is there another way to remove them?
 
Old 11-15-2007, 12:42 AM   #5
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
In sed & awk, you can precede a command to select just the lines you want, and then in sed use the substitute command, or in awk print out just the fields you need.

Code:
sed -n '3s/<td>\(.*\) <\/td>/\1/p;6s/<td>\(.*\) <\/td>/\1/p' sample
21.7 kb/s (0.9%)
7768.0 b/s (0.3%)
The example given before, "sed -n '3,6p' file", will actually print the lines between 3 and 6 inclusive.

This might be a better version:
Code:
sed -n 's/<td>\(.*\) <\/td>/\1/;3p;6p;7q' sample
21.7 kb/s (0.9%)
7768.0 b/s (0.3%)
Every line has the tags removed but only lines 3 & 6 are printed. When it gets to line 7, sed quits. This can save time if you have a very long file.

Note that there is a slight irregularity in your sample file. On the 19th line there isn't a space before the end tag. If this wasn't a mistake, then remove the space before the endtag in the sed program to handle such a case:
example:
Code:
sed -n 's/<td>\(.*\)<\/td>/\1/;3p;6p;19p;20q' sample
Otherwise the tags will be in the output. This way however, there will be a space at the end of the line.

Or you could add another sed command to remove any trailing spaces:
Code:
sed -n 's/<td>\(.*\)<\/td>/\1/;s/ *$//;3p;6p;19p;20q' sample

Last edited by jschiwal; 11-15-2007 at 12:53 AM.
 
Old 11-15-2007, 01:59 AM   #6
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Quote:
Originally Posted by packets View Post
I tried this:

cat test.txt | awk ' NR == 1 || NR == 9 {print }' # 1 and 9 are the column

<td>231.7 kb/s (9.7%)</td>
<td>16.8 kb/s (0.7%) </td>

It works. The limitation here is how to get rid of <td> and show only 231.7 kb/s and 16.8 kb/s?
If only there is a space after the > , there could be no problem since awk/cut can remove this but since not all has no space, is there another way to remove them?
Stop the cat'ing, will you? :}

If you find yourself using cat with only one argument you're
abusing it.

Code:
awk -F'(<td>|</td>)' 'NR == 1 || NR == 9 {print $2}'  test.txt
And 1 and 9 are the rows.



Cheers,
Tink

Last edited by Tinkster; 11-15-2007 at 02:00 AM.
 
  


Reply



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
how to grep multiple filters with grep LinuxLover Linux - Enterprise 1 10-18-2007 07:12 AM
Transposing a column into a row mayyash Linux - General 1 09-30-2005 02:23 AM
ordering row entries rohr Programming 4 04-11-2005 03:36 AM
MPlayer - Green Row Micro420 Linux - Software 4 10-13-2003 10:14 AM
ps -ef|grep -v root|grep apache<<result maelstrombob Linux - Newbie 1 09-24-2003 11:38 AM

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

All times are GMT -5. The time now is 05:15 PM.

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
Open Source Consulting | Domain Registration