LinuxQuestions.org
Review your favorite Linux distribution.
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 09-07-2007, 08:19 AM   #1
mruknown1
LQ Newbie
 
Registered: Sep 2007
Posts: 6

Rep: Reputation: 0
using grep command


let says the file 'fileabc' content is

7 friday sept 2007
8 saturday sept 2007
9 sunday sept 2007
10 monday sept 2007
11 tuesday sept 2007
12 wednesday sept 2007
13 thursday sept 2007
14 friday sept 2007

5 friday oct 2007
6 saturday oct 2007
7 sunday oct 2007
8 monday oct 2007
9 tuesday oct 2007
10 wednesday oct 2007
11 thursday oct 2007
12 friday oct 2007

what is the grep command if i want to print a line with combination of text or number in a 'fileabc' ?
lets say i want to find a line that contain 'sept friday 2007'
 
Old 09-07-2007, 08:45 AM   #2
AlucardZero
Senior Member
 
Registered: May 2006
Location: USA
Distribution: Debian
Posts: 4,650

Rep: Reputation: 525Reputation: 525Reputation: 525Reputation: 525Reputation: 525Reputation: 525
That's easy.. have you read the man page? Type man grep and take a look, and if that looks too scary Google for a grep tutorial.

That said, if you want to find lines that contain "sept friday 2007" in file "fileabc", it's a simple
Code:
grep "sept friday 2007" fileabc
If you want to search case-insensitivly, it's
Code:
grep -i "sept friday 2007" fileabc
etc etc.
 
Old 09-07-2007, 09:11 AM   #3
mruknown1
LQ Newbie
 
Registered: Sep 2007
Posts: 6

Original Poster
Rep: Reputation: 0
acctually, friday sept 2007 is not in order. there is some text in between. so is there any other command?
 
Old 09-07-2007, 09:32 AM   #4
pam196
LQ Newbie
 
Registered: Sep 2007
Posts: 12

Rep: Reputation: 0
you could try pipeing grep commands together e.g. :
grep friday fileabc|grep sept fileabc|grep 2007 fileabc
Each grep should filter the previous one and at the end it should only return the lines containing all three. I'm sure there's a much cleaner way of passing it to grep...
more fileabc|grep friday|grep sept|grep 2007
and thats probably still long winded.

Apologies if the answers off the mark - I dont have access to a *nix system at the moment, and have been away from linux administration for about 7 years now!
 
Old 09-07-2007, 09:34 AM   #5
AlucardZero
Senior Member
 
Registered: May 2006
Location: USA
Distribution: Debian
Posts: 4,650

Rep: Reputation: 525Reputation: 525Reputation: 525Reputation: 525Reputation: 525Reputation: 525
Quote:
Originally Posted by mruknown1 View Post
acctually, friday sept 2007 is not in order. there is some text in between. so is there any other command?
You should also search for a beginner's regular expression tutorial, because we're not here to answer every little question all day, but to help you learn.
Code:
grep "sept.+?friday.+?2007" fileabc
 
Old 09-07-2007, 09:45 AM   #6
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
Quote:
because we're not here to answer every little question all day
Sure we are--we do it all the time....
Quote:
you could try pipeing grep commands together e.g. :
grep friday fileabc|grep sept fileabc|grep 2007 fileabc
If you are doing this, I don't think you would repeat the filename in each grep

More readable is:
cat filename|grep friday|grep sept|grep 2007
 
Old 09-10-2007, 07:40 AM   #7
mruknown1
LQ Newbie
 
Registered: Sep 2007
Posts: 6

Original Poster
Rep: Reputation: 0
thanks pam196...
1st command is not too exact. but the 2nd command give what i need. thanks for the command. i'm new to this forum but my 1st impression shows this forum give a +ve feedback very fast. thanks again..
 
Old 09-10-2007, 08:17 AM   #8
mruknown1
LQ Newbie
 
Registered: Sep 2007
Posts: 6

Original Poster
Rep: Reputation: 0
Hello... i need awk/perl script that can calculate packet size.

for example based on this output, each column have their own meaning
column1=send/received, drop,...
column2= timestamp
column3=node
column4=level
column5=flag
column6=packetID
column7=packettype
column8=packetsize
.....
.....

1. How if i want to calculate total PACKETSIZE RECEIVED by NODE 5 with RTR LEVEL and DSR PACKETTYPE .From here i will try to manipulate the script so i can used it in my research.

2. How if i want to extract this output so i can create a new file that print 2 columns (TIMESTAMP and DSR) when NODE 5 RECEIVED DSR PACKETTYPE with RTR LEVEL.

thank you very much for any help...

s 84.456630640 _5_ AGT --- 0 cbr 512 [0 0 0 0] ------- [5:1 6:1 32 0] [0] 0 1
r 84.456630640 _5_ RTR --- 0 cbr 512 [0 0 0 0] ------- [5:1 6:1 32 0] [0] 0 1
s 84.460452677 _5_ RTR --- 1 DSR 32 [0 0 0 0] ------- [5:255 6:255 32 0] 1 [1 1] [0 1 0 0->0] [0 0 0 0->0]
r 84.461724861 _4_ RTR --- 1 DSR 32 [0 ffffffff 5 800] ------- [5:255 6:255 32 0] 1 [1 1] [0 1 0 0->0] [0 0 0 0->0]
r 84.461724909 _0_ RTR --- 1 DSR 32 [0 ffffffff 5 800] ------- [5:255 6:255 32 0] 1 [1 1] [0 1 0 0->0] [0 0 0 0->0]
r 84.461725322 _6_ RTR --- 1 DSR 32 [0 ffffffff 5 800] ------- [5:255 6:255 32 0] 1 [1 1] [0 1 0 0->0] [0 0 0 0->0]
r 84.461725410 _1_ RTR --- 1 DSR 32 [0 ffffffff 5 800] ------- [5:255 6:255 32 0] 1 [1 1] [0 1 0 0->0] [0 0 0 0->0]
r 84.461725471 _2_ RTR --- 1 DSR 32 [0 ffffffff 5 800] ------- [5:255 6:255 32 0] 1 [1 1] [0 1 0 0->0] [0 0 0 0->0]
s 84.462207082 _6_ RTR --- 2 DSR 44 [0 0 0 0] ------- [6:255 5:255 254 5] 2 [0 1] [1 1 2 5->6] [0 0 0 0->0]
r 84.466670604 _5_ RTR --- 2 DSR 44 [13a 5 6 800] ------- [6:255 5:255 254 5] 2 [0 1] [1 1 2 5->6] [0 0 0 0->0]
SFESTs 84.466670604 _5_ 0 [5 -> 6] 1(1) to 6 [5 |6 ]
s 84.466670604 _5_ RTR --- 0 cbr 548 [0 0 0 0] ------- [5:1 6:1 32 6] [0] 0 1
r 84.472832542 _6_ RTR --- 0 cbr 548 [13a 6 5 800] ------- [5:1 6:1 32 6] [0] 1 1
r 84.472832542 _6_ AGT --- 0 cbr 512 [13a 6 5 800] ------- [5:1 6:1 32 6] [0] 1 1
s 89.865825846 _5_ AGT --- 3 cbr 512 [0 0 0 0] ------- [5:1 6:1 32 0] [1] 0 2
r 89.865825846 _5_ RTR --- 3 cbr 512 [0 0 0 0] ------- [5:1 6:1 32 0] [1] 0 2
SFESTs 89.865825846 _5_ 3 [5 -> 6] 1(1) to 6 [5 |6 ]
s 89.865825846 _5_ RTR --- 3 cbr 548 [0 0 0 0] ------- [5:1 6:1 32 6] [1] 0 2
SSendFailure 89.907170846 _5_ 3 2 5:1 6:1 5->6 2 2 2 2 [|5 6 ]
s 89.912216246 _5_ RTR --- 4 DSR 32 [0 0 0 0] ------- [5:255 6:255 32 0] 1 [1 2] [0 2 0 0->0] [0 0 0 0->0]
r 89.913368448 _0_ RTR --- 4 DSR 32 [0 ffffffff 5 800] ------- [5:255 6:255 32 0] 1 [1 2] [0 2 0 0->0] [0 0 0 0->0]
s 89.913368448 _0_ RTR --- 5 DSR 48 [0 0 0 0] ------- [0:255 5:255 255 5] 2 [0 2] [1 2 3 5->6] [0 0 0 0->0]
r 89.913368578 _4_ RTR --- 4 DSR 32 [0 ffffffff 5 800] ------- [5:255 6:255 32 0] 1 [1 2] [0 2 0 0->0] [0 0 0 0->0]
s 89.913368578 _4_ RTR --- 6 DSR 48 [0 0 0 0] ------- [4:255 5:255 255 5] 2 [0 2] [1 2 3 5->6] [0 0 0 0->0]
r 89.913368941 _1_ RTR --- 4 DSR 32 [0 ffffffff 5 800] ------- [5:255 6:255 32 0] 1 [1 2] [0 2 0 0->0] [0 0 0 0->0]
s 89.913368941 _1_ RTR --- 7 DSR 48 [0 0 0 0] ------- [1:255 5:255 255 5] 2 [0 2] [1 2 3 5->6] [0 0 0 0->0]
r 89.913369041 _2_ RTR --- 4 DSR 32 [0 ffffffff 5 800] ------- [5:255 6:255 32 0] 1 [1 2] [0 2 0 0->0] [0 0 0 0->0]
r 89.919259468 _5_ RTR --- 7 DSR 48 [13a 5 1 800] ------- [1:255 5:255 255 5] 2 [0 2] [1 2 3 5->6] [0 0 0 0->0]
SFESTs 89.919259468 _5_ 3 [5 -> 6] 3(1) to 1 [5 |1 6 ]
s 89.919259468 _5_ RTR --- 3 cbr 552 [13a 6 5 800] ------- [5:1 6:1 32
 
Old 09-10-2007, 11:53 AM   #9
synss
Member
 
Registered: Jul 2007
Location: Germany
Distribution: Arch Linux, Mac OS X
Posts: 132

Rep: Reputation: 22
uh... what about searching "awk tutorial" in google, the first hit brings you http://www.vectorsite.net/tsawk_1.html#m2 and your problem is solved even faster than asking the forums!
 
Old 09-11-2007, 07:37 AM   #10
pam196
LQ Newbie
 
Registered: Sep 2007
Posts: 12

Rep: Reputation: 0
OK, I'm well bored today...as you've probably already worked this one out for yourself...

1. more filename|grep "^r"|grep "_5_"|grep "RTR"|awk '$7 == "DSR" { sum += $8 } END {print sum} '

the above should get lines beginning with "r" (thats what the ^ does), filter as requested using greps, awk then matches the remaining lines that have DSR in column 7 and adds the content of column 8 to the variable sum. The final output is the total number of column 8.

To redirect output from standard output (terminal screen) to a file, at the end of the command use > filename

2. more filename|grep "^r"|grep "_5_"|grep "RTR"|grep "DSR"|awk ' { print $2 " " $7 } ' > newfilename

Basically that should be it. BTW I put a space in the awk output (print $2 " " $7 ), otherwise awk would cat the two columns together. The space is acheived by the " " .

Hope this helps and that you're learning something...other than to post all questions on a forum before checking out google

Last edited by pam196; 09-11-2007 at 07:40 AM. Reason: accidental submittal
 
Old 09-11-2007, 11:57 AM   #11
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
Code:
awk '$3=="_5_" && $4 == "RTR" && $7 == "DSR" { c=c+$8 }END{print c}' "file"
 
Old 09-11-2007, 12:06 PM   #12
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
Quote:
Originally Posted by pam196 View Post
1. more filename|grep "^r"|grep "_5_"|grep "RTR"|awk '$7 == "DSR" { sum += $8 } END {print sum} '
there's no need for "more" and if really want to use grep, why not this
Code:
grep "^r.*_5_.*RTR.*DSR" file | awk '.....'
 
  


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
grep command thodo Programming 12 05-15-2007 08:07 PM
how to use grep command sharonyiisl Linux - Newbie 7 05-28-2006 04:46 PM
Help Use Grep Command, Thanks! zzytech Linux - Newbie 2 04-06-2006 12:37 AM
grep command itz2000 Linux - Newbie 2 09-21-2005 08:06 PM
grep command in c????? alnreddy Linux - Software 1 12-30-2004 02:01 AM


All times are GMT -5. The time now is 03:51 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration