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 |
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.
|
 |
08-13-2009, 09:05 AM
|
#1
|
LQ Newbie
Registered: Aug 2009
Posts: 5
Rep:
|
is it awk -v or sub the problem ?
Hi
any enlightenment will help .
I am trying to substitute a string with a replacement for the 2nd occurence of it for each line.
Here's my command:
$ awk -v x=$strA -v y=$strB -v z=$strC '{ if ( match($2,z) > 0 ) { sub( /x/,y,$2) } ; print }' abc.dat
It finds the match alright, but the substitution refuses to work and print faithfully prints every line in the
file unchanged.
I am very surprised !? !
output of the above command : abc.dat is printed out as-is !!
$ awk -v x=$strA -v y=$strB -v z=$strC '{ if ( match($2,z) > 0 ) { sub( /x/,y,$2) } ; print }' abc.dat
starthere:server=REP60_PROD userid=AAAA/aaaa@PROD field1=value1 field2=value2 field3=value3 field4=value4 andso on....
start2here:server=REP60_PROD userid=AAAA/aaaa@PROD field1=value1 field2=value2 field3=value3 field4=value4 andso on....
regards to the gurus,
a fellow human being
|
|
|
08-13-2009, 11:57 AM
|
#2
|
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852
|
Remove the slashes from around the match in sub(). They treat the value inside of them as a regex string, so you're trying to match a literal 'x'. Without slashes it's treated as a variable containing the expression to be matched, which is what you want.
Code:
awk -v x=$strA -v y=$strB -v z=$strC '{ if ( match($2,z) > 0 ) { sub( x,y,$2) } ; print }' abc.dat
PS: It's best to put all but the most trivial code strings inside [code][/code] tags. It preserves formatting (of both the code and the page) and makes things easier to read.
Last edited by David the H.; 08-13-2009 at 12:06 PM.
|
|
|
08-13-2009, 12:48 PM
|
#3
|
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809
|
Quote:
I am trying to substitute a string with a replacement for the 2nd occurence of it for each line.
|
If I am understanding you correctly, how about this:
sed 's/string/replacement/2' filename
replaces the 2nd instance of "string" with "replacement"
|
|
|
08-13-2009, 06:02 PM
|
#4
|
LQ Newbie
Registered: Aug 2009
Posts: 5
Original Poster
Rep:
|
learning to awk this time
Thank you for reply. I was tempted to use sed. I am familiar with sed with 's/ / / ' . But I was keen on learning to awk this time. Appreciate the early response.
Quote:
Originally Posted by pixellany
If I am understanding you correctly, how about this:
sed 's/string/replacement/2' filename
replaces the 2nd instance of "string" with "replacement"
|
|
|
|
08-13-2009, 06:09 PM
|
#5
|
LQ Newbie
Registered: Aug 2009
Posts: 5
Original Poster
Rep:
|
was it that simple !? yes, it worked !!
Yes absolutely. It worked ! Thank you for taking the time to reply to my post. I hope to keep posting here as a way of learning and keeping in touch. Thanks again.
Quote:
Originally Posted by David the H.
Remove the slashes from around the match in sub(). They treat the value inside of them as a regex string, so you're trying to match a literal 'x'. Without slashes it's treated as a variable containing the expression to be matched, which is what you want.
Code:
awk -v x=$strA -v y=$strB -v z=$strC '{ if ( match($2,z) > 0 ) { sub( x,y,$2) } ; print }' abc.dat
PS: It's best to put all but the most trivial code strings inside [code][/code] tags. It preserves formatting (of both the code and the page) and makes things easier to read.
|
|
|
|
All times are GMT -5. The time now is 05:14 AM.
|
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
|
|