Operating System - HP-UX
1830207 Members
1163 Online
109999 Solutions
New Discussion

replacement string in awk

 
Jdamian
Respected Contributor

replacement string in awk

Hi

Do the substitution strings as \n work in sub() and gsub() awk functions ?

$ echo "A B" | awk '{ sub("A","\\1"); print }'
\1 B

P.D: '&' works fine
1 REPLY 1
curt larson_1
Honored Contributor

Re: replacement string in awk

the \n notation where \n matches the nth pattern previously saved by "\(" and "\)" is from the unix command sed, not from awk

for awk you'd do something like this:

a=abc
b=def
print "$a $b" |
awk '{
sub($1,$2);
print $1;
}'

with awk each input line forms a record containing any number of fields. each field can be referenced by its position in the record. $1 refers to the value of the first field; $2 to the second field, and so on. $0 refers to the entire record.