Alex Brandt
Alex Brandt

Reputation: 99

Substitute in only field 3

What's wrong with my syntax here?

awk -F '|' 'sub/\s*\w*/,"Visit our website!","$3"' merchant_report

it's suppose to turn

|bob|jones| blagblag| texas
|tom|markus| | alabama

into

|bob|jones|Visit our website!| texas
|tom|markus| | alabama

Upvotes: 0

Views: 49

Answers (2)

Chris Seymour
Chris Seymour

Reputation: 85865

In awk you would just assign the field the new value for the given line. If you are more comfortable with the substitution approach try sed:

sed '1s/|[^|]*/|Visit our website!/3' file
|bob|jones|Visit our website!| texas
|tom|markus| | alabama

Upvotes: 0

Kent
Kent

Reputation: 195209

this line may do what you want:

awk -F'|' -v OFS="|" 'NR==1{$4="Visit our website!"}1' file

in your awk codes:

  • you have had FS to separate the fields, you don't need the sub func., just set $3 directly.
  • even if with sub( ) function, your syntax is not correct. you can get detail info by man gawk
  • it is actually not $3, it is $4. because your line starting with |
  • if you want to just change the first line, you should add NR==1 otherwise awk will do the change on all lines

example with the code:

kent$  cat file
    |bob|jones| blagblag| texas
    |tom|markus| | alabama

kent$  awk -F'|' -v OFS="|" 'NR==1{$4="Visit our website!"}1' file
    |bob|jones|Visit our website!| texas
    |tom|markus| | alabama

Upvotes: 1

Related Questions