LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 01-31-2011, 06:21 PM   #1
schneidz
Senior Member
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 4,095

Rep: Reputation: 635Reputation: 635Reputation: 635Reputation: 635Reputation: 635Reputation: 635
bash- sed, awk, grep


hi, lets say i got a file like
Code:
hi my name is simon
and i like to do drawings
are you looking at my bum
l33t h4x0rz simon chun-li
madam, i'm adam
and i want to enumerate the simon's so it looks like:
Code:
hi my name is simon-1
and i like to do drawings
are you looking at my bum
l33t h4x0rz simon-2 chun-li
madam, i'm adam
obviously, sed s/simon/simon-x/ wont help me.

can someone please give me some ideas ?
thankx,
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 01-31-2011, 07:04 PM   #2
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,604

Rep: Reputation: 446Reputation: 446Reputation: 446Reputation: 446Reputation: 446
Hi,

here is an awk solution
Code:
awk '/simon/{c++; gsub("simon","simon-"c)}1' dummy
or even shorter
Code:
awk '/simon/{gsub("simon","simon-"++c)}1' dummy
 
2 members found this post helpful.
Old 01-31-2011, 07:22 PM   #3
kurumi
Member
 
Registered: Apr 2010
Posts: 223

Rep: Reputation: 45
Quote:
Originally Posted by crts View Post
Hi,

here is an awk solution
Code:
awk '/simon/{c++; gsub("simon","simon-"c)}1' dummy
or even shorter
Code:
awk '/simon/{gsub("simon","simon-"++c)}1' dummy
that don't work if there are more than 1 simon in one line
 
Old 01-31-2011, 07:40 PM   #4
kurumi
Member
 
Registered: Apr 2010
Posts: 223

Rep: Reputation: 45
Ruby(1.9+)

Code:
ruby -F"\s" -ane 'BEGIN{c=0};$F.map!{|x| x["simon"]? x="#{x}#{c+=1}":x=x };puts $F.join("\s")' file
 
Old 01-31-2011, 07:47 PM   #5
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,604

Rep: Reputation: 446Reputation: 446Reputation: 446Reputation: 446Reputation: 446
Quote:
Originally Posted by kurumi View Post
that don't work if there are more than 1 simon in one line
True. But it is not obvious from the OP's example that there will be more than one Simon in a line. And from the sample data provided is also not obvious how multiple Simons in a line should be handled. Until further clarification I like to keep it as simple as possible.
 
Old 01-31-2011, 07:52 PM   #6
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,564

Rep: Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939
Quote:
Originally Posted by kurumi
that don't work if there are more than 1 simon in one line
I beg to differ seeing that the 'g' in gsub stands for global changes so all would be included.
What you might have meant to say is that subsequent simon's on the same line will have the same number at the end

Just to clarify

So an alternative would be:
Code:
awk '/simon/{for(i=1;i<=NF;i++)if($i == "simon")$i = $i"-"++c}1' file
 
1 members found this post helpful.
Old 01-31-2011, 08:03 PM   #7
schneidz
Senior Member
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 4,095

Original Poster
Rep: Reputation: 635Reputation: 635Reputation: 635Reputation: 635Reputation: 635Reputation: 635
Quote:
Originally Posted by crts View Post
True. But it is not obvious from the OP's example that there will be more than one Simon in a line. And from the sample data provided is also not obvious how multiple Simons in a line should be handled. Until further clarification I like to keep it as simple as possible.
only 1 simon per line... i guess multiple simons would be worth bonus points.
(reputation added)
 
Old 01-31-2011, 08:05 PM   #8
schneidz
Senior Member
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 4,095

Original Poster
Rep: Reputation: 635Reputation: 635Reputation: 635Reputation: 635Reputation: 635Reputation: 635
Quote:
Originally Posted by kurumi View Post
Ruby(1.9+)

Code:
ruby -F"\s" -ane 'BEGIN{c=0};$F.map!{|x| x["simon"]? x="#{x}#{c+=1}":x=x };puts $F.join("\s")' file
thanks for offering your suggestion. unfortunately, my corperations installation of aix doesnt have ruby so i couldnt test it out.
 
Old 01-31-2011, 08:33 PM   #9
kurumi
Member
 
Registered: Apr 2010
Posts: 223

Rep: Reputation: 45
Quote:
Originally Posted by grail View Post
What you might have meant to say is that subsequent simon's on the same line will have the same number at the end
yes, that's what I meant.
 
Old 01-31-2011, 08:48 PM   #10
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,564

Rep: Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939
Same, same, but a little bit different
Code:
awk '/simon/{gsub(/simon/,"&-"++c)}1' file
 
1 members found this post helpful.
Old 01-31-2011, 08:56 PM   #11
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,604

Rep: Reputation: 446Reputation: 446Reputation: 446Reputation: 446Reputation: 446
Quote:
Originally Posted by grail View Post
Same, same, but a little bit different
Code:
awk '/simon/{gsub(/simon/,"&-"++c)}1' file
I was suspecting that awk must have an equivalent to sed's backreference mechanism.
 
  


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 help or sed or awk dmchess Linux - Software 4 09-29-2010 06:53 PM
[SOLVED] Help using awk,sed and grep shakes82 Programming 34 07-07-2010 11:12 PM
help with grep/sed/awk nikunjbadjatya Programming 8 02-17-2010 07:29 PM
bash - awk, sed, grep, ... advice schneidz Programming 13 08-25-2008 09:30 AM
bash script with grep and sed: sed getting filenames from grep odysseus.lost Programming 1 07-17-2006 11:36 AM


All times are GMT -5. The time now is 02:54 AM.

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