I am facing problem when trying to substitute the first line of a text file using awk command in Unix.

I am using this syntax:

awk '{ (FS=OFS=""); if($19=="2") sub($88,"4");print $0 }' input.txt>output.txt

this command is not editing the first line of the text file. what is the reason behind this?


It's hard to understand what you are trying to do without an example of your input and expected output (in particular, sub($88,"4") looks dubious - the first argument of sub should be a regular expression).

However, assuming you want an empty FS to be applied to the first record (line) of your input, then you need to set it before the first record is parsed - using a BEGIN rule. For example, compare

$ printf '123\n456\n' | awk '{FS=OFS=""; print $2}'



$ printf '123\n456\n' | awk 'BEGIN{FS=OFS=""} {print $2}'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.