Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

awk string substituion exception

awk string substituion exception

awk string substituion exception

Hello awk experts, I want to substitute the data.txt file according to the template.txt file rules:

template.txt contains the following:
hello hola
one uno
two dos
three tre
four quatro
goodbye adios

data.txt contains the following:
hello my name is David
one of my friends are John
/two three
hello my number two friend is Mark

substituted.txt contains:
hola my name is David
uno of my friends are John
/dos tres
hola my number dos friend is Mark

I got this command to work:
awk 'NR==FNR {a[$1]=$2;next} {for ( i in a ) gsub(i,a[i])}1' templates.txt data.txt > substituted.txt

But I want to skip the string that starts with "/" to NOT substitue (ie. remain as /two) so I added the !/^\//
awk 'NR==FNR {a[$1]=$2;next} {for ( i in a ) !/^\//; gsub(i,a[i])}1' templates.txt data.txt > substituted.txt

Now, this command doesn't substitue at all now. What am I doing wrong?

Any help is greatly appreciated. Thank you!

RE: awk string substituion exception


Quote (pdtak1)

What am I doing wrong?
for executes the first command that follows it :


{for ( i in a ) !/^\//; gsub(i,a[i])} 

The easiest fix is to tell to execute gsub() only if the match was successful :


{for ( i in a ) !/^\// && gsub(i,a[i])} 


RE: awk string substituion exception

Thank you feherke!
One more question, now if I only want to substitute the 4th field of the data.txt file (ignore the first 3 fields), then where would I tell it to check only the 4th field (or ignore the first 3 fields)?

RE: awk string substituion exception


gsub() can take a 3rd parameter, a variable to process. By default is $0. If you want to process only $4, just tell it :


{for ( i in a ) !/^\// && gsub(i,a[i],$4)} 


RE: awk string substituion exception


I'm still having issues skipping the first column
{for ( i in a ) !/^\// && gsub(i,a[i])} didn't work.

I want to skip the first column but substitue all remaining columns... can't get it working.

awk 'NR==FNR {a[$1]=$2;next} {for ( i in a) gsub(i,a[i],!$1); printf $1}1'
awk 'NR==FNR {a[$1]=$2;next} {for ( i in a ) !/^\// && gsub(i,a[i])}; printf $1}1'

None give me the results I want... skip the first column, then substitute remaining columns.
Please help.

RE: awk string substituion exception


I am afraid I did not understood you correctly. Actually you want to replace all matching strings excepting those prefixed with slash ( / ) ?

CODE --> template.txt

one uno
two dos 

CODE --> data.txt

one \two three \one two three 

CODE --> substituted.txt

uno \two three \one dos three 

Well, this is more complicated as Awk regular expressions does not handle negative look-behind assertion.

The simplest workaround is to temporarily replace the slash prefixed occurrences to something else ( something you are sure is not present in your text - here I use the string "--KEEP--" ), then replace the remaining, un-prefixed occurrences, then replace that placeholder back :


{for ( i in a ) {gsub("/"i,"--KEEP--"); gsub(i,a[i]); gsub("--KEEP--","/"i)}} 

Please note that your original code also transforms for example "bayonet" to "bayunot", which is done by the modified code too. Not sure if that was your intention or not.


RE: awk string substituion exception

Wow, that worked perfectly!
Thank you so much!
You're the best feherke!

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close