Explanation of print statement - awk

Hi, i'm just after a simple explanation of how the following awk oneliner works.

 
gawk -F"," '{for(i=m;i<=n;i++)printf "%s" OFS,$i; print x}' m=1 n=70 OFS=, input.csv > output.csv

In particular i'm trying to understand how the two print statements work? How is the "x" variable being assigned a value? Is the "x" variable being assigned the value of the first printf statement?

Thanks in advance,
TheFlamingMoe

I can see why you're puzzled, that took some thought.

Nothing at all is assigned to X. It's printing a newline. print "" would have the same effect, but they saved one character by using a blank variable. Just print all by itself with no parameters would print the entire line, not what you want.

The printf statements do not print newlines, those must be explicit when using printf. So it prints a bunch of things into one long line, then uses print "" to finish off the line.

Are you sure that's OFS,$i and not OFS $i ? Two things into a single %s is a syntax error as far as I know.

Thanks Corona688, you answered my question perfectly. The comma in the statement "OFS,$i" was in the original script, but it's probably a typo. I'll check it out. Thanks again :wink:

TheFlamingMoe

---------- Post updated at 05:23 AM ---------- Previous update was at 05:08 AM ----------

Checked out the above query. The awk statement doesn't work without the comma. Add the comma and things work perfectly. So I guess it's not a typo!!!

It isn't a syntax error, but it isn't common. The awk printf command:

printf "%s" OFS,$i

uses the concatenation of "%s" and OFS as a format string; together they print the contents of the ith input field followed by the output field separator. If there is a bug here, it is that there is an extraneous trailing field separator printed at the end of each output line before the <newline> added by the print x command.

2 Likes