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 06-30-2009, 08:45 PM   #1
srini_nw
LQ Newbie
 
Registered: May 2009
Posts: 1

Rep: Reputation: 0
Cool searching using sed ...


Hi,i was actually having a little trouble using sed command (in fedora8).
How do I search a search a file for a particular string in it and to track several occurances.
Please help me out.Thanks.
 
Old 06-30-2009, 08:52 PM   #2
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
You might actually want to be using GREP rather than SED.
Sed is a stream editor, which allows in-stream editing of text or file(s), while GREP is for locating occurrences of stuff in the files but not changing it.

(There's more to each, but that's the basic idea.)

You could also use GAWK, and probably AWK too, but GREP should do what you want. If not, post more information about what you want to do with the found occurrences.

Sasha
 
Old 06-30-2009, 08:55 PM   #3
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Code:
awk '/searchpattern/{d++} END {print "total count: "d}'
 
Old 06-30-2009, 08:57 PM   #4
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
Like I sed, awk will probably do it too.

Quote:
Originally Posted by ghostdog74 View Post
Code:
awk '/searchpattern/{d++} END {print "total count: "d}'
There! See? A perfectly fine example of why I need to learn AWK. How cool & simple is that!?

Thanks Ghostdog
 
Old 06-30-2009, 09:04 PM   #5
micxz
Senior Member
 
Registered: Sep 2002
Location: CA
Distribution: openSuSE, Cent OS, Slackware
Posts: 1,131

Rep: Reputation: 75
Quote:
Originally Posted by ghostdog74 View Post
Code:
awk '/searchpattern/{d++} END {print "total count: "d}'
You could get this typing less by
grep searchpattern | wc -l
but I think the poster just wants grep as suggested
But great example none the less I'm impressed'

Last edited by micxz; 06-30-2009 at 09:05 PM.
 
Old 06-30-2009, 09:15 PM   #6
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by micxz View Post
You could get this typing less by
grep searchpattern | wc -l
but I think the poster just wants grep as suggested
But great example none the less I'm impressed'
its even less if you use grep's -c option. Even if OP's requirement is that simple, awk can still be used. Also it provides more capabilities than grep has. Therefore, one good tool is all you need to get the job done.
 
Old 06-30-2009, 10:29 PM   #7
micxz
Senior Member
 
Registered: Sep 2002
Location: CA
Distribution: openSuSE, Cent OS, Slackware
Posts: 1,131

Rep: Reputation: 75
true again' I would like to learn more about how to use awk
 
Old 06-30-2009, 10:32 PM   #8
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by micxz View Post
true again' I would like to learn more about how to use awk
see my sig last second link
 
  


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, sed, awk or tr - searching words in a string hal8000b Programming 2 03-06-2009 08:04 PM
bash script with grep and sed: sed getting filenames from grep odysseus.lost Programming 1 07-17-2006 11:36 AM
[sed] "Advanced" sed question(s) G00fy Programming 2 03-20-2006 12:34 AM
sed and escaping & in something like: echo $y | sed 's/&/_/g' prx Programming 7 02-03-2005 11:00 PM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 06:12 AM


All times are GMT -5. The time now is 07:26 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