LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 08-13-2009, 09:05 AM   #1
sandlinux
LQ Newbie
 
Registered: Aug 2009
Posts: 5

Rep: Reputation: 0
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
 
Old 08-13-2009, 11:57 AM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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.
 
Old 08-13-2009, 12:48 PM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
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"
 
Old 08-13-2009, 06:02 PM   #4
sandlinux
LQ Newbie
 
Registered: Aug 2009
Posts: 5

Original Poster
Rep: Reputation: 0
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 View Post
If I am understanding you correctly, how about this:

sed 's/string/replacement/2' filename

replaces the 2nd instance of "string" with "replacement"
 
Old 08-13-2009, 06:09 PM   #5
sandlinux
LQ Newbie
 
Registered: Aug 2009
Posts: 5

Original Poster
Rep: Reputation: 0
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. View Post
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.
 
  


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
Help with AWK problem RyudoBlaze Programming 3 04-24-2009 07:24 PM
shell command using awk fields inside awk one71 Programming 6 06-26-2008 04:11 PM
A problem about awk jackk294 Programming 4 07-13-2006 08:56 PM
AWK problem embsupafly Programming 13 08-21-2005 07:43 PM
awk problem alaios Programming 4 05-04-2003 09:46 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:14 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
Open Source Consulting | Domain Registration