awk

Hi Gurus,

I have mostly understood the code and tweaked according to my needs. I didn't understand the use of '1' in the below code. I tried to remove it but I don't get any output once I remove 1. I googled and referred to a few websites but didn't find the required information. I would appreciate your help.

awk 'BEGIN{FS=OFS=","}  {
   for (i=1; i<=NF; i++) {gsub(/^"|"$/, "", $i); $i = "\"" $i "\""}
} 1' file

Please use code tags for code, icode is for single-line things.

awk syntax is very compact because it has its own built in loop and shortcuts.

A simple awk program might look like:

awk 'expression1 { code } expression2' inputfile

Which, including awk's built in loop, looks like this pseudocode:

while inputfile is not empty
{
        read line
        if (expression1) run code
        if (expression2) print line
}

If expression1 is missing, the code block is always run.

The 1 in your code is expression2. Since it's always nonzero, or true, it always prints the line back out -- after all that substitution is done to it.

1 Like