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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
01-31-2011, 06:21 PM
|
#1
|
LQ Guru
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,330
|
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.
|
01-31-2011, 07:04 PM
|
#2
|
Senior Member
Registered: Jan 2010
Posts: 2,020
|
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.
|
01-31-2011, 07:22 PM
|
#3
|
Member
Registered: Apr 2010
Posts: 228
Rep:
|
Quote:
Originally Posted by crts
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
|
|
|
01-31-2011, 07:40 PM
|
#4
|
Member
Registered: Apr 2010
Posts: 228
Rep:
|
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
|
|
|
01-31-2011, 07:47 PM
|
#5
|
Senior Member
Registered: Jan 2010
Posts: 2,020
|
Quote:
Originally Posted by kurumi
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.
|
|
|
01-31-2011, 07:52 PM
|
#6
|
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,040
|
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.
|
01-31-2011, 08:03 PM
|
#7
|
LQ Guru
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,330
Original Poster
|
Quote:
Originally Posted by crts
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)
|
|
|
01-31-2011, 08:05 PM
|
#8
|
LQ Guru
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,330
Original Poster
|
Quote:
Originally Posted by kurumi
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.
|
|
|
01-31-2011, 08:33 PM
|
#9
|
Member
Registered: Apr 2010
Posts: 228
Rep:
|
Quote:
Originally Posted by grail
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.
|
|
|
01-31-2011, 08:48 PM
|
#10
|
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,040
|
Same, same, but a little bit different
Code:
awk '/simon/{gsub(/simon/,"&-"++c)}1' file
|
|
1 members found this post helpful.
|
01-31-2011, 08:56 PM
|
#11
|
Senior Member
Registered: Jan 2010
Posts: 2,020
|
Quote:
Originally Posted by grail
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.
|
|
|
All times are GMT -5. The time now is 07:31 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|