Understanding awk behavior


New Member
Hi all,

I'm very new to awk and I'm taking a course to learn the basics. There's this exercise where you have to generate a tab-separated file using a comma-separated file as a source.

Here's the original file:
Art Venere,[email protected],256.62394383
Lenna Paprocki,[email protected],259.688783099
Donette Foller,[email protected],282.32979844
Simona Morasca,[email protected],139.51911647
Dominque Dickerson,[email protected],167.045197551
Leota Dilliard,[email protected],253.646335223
Sage Wieser,[email protected],155.55576823
Kris Cho,[email protected],210.794277775
Minna Amigon,[email protected],95.47955397
Abel Maclead,[email protected],225.774477397
Kiley Caldarera,[email protected],172.957628871
Graciela Ruta,[email protected],202.68364784
Josephine Darakjy,[email protected],178.877840188
Cammy Albares,[email protected],290.446513401
Mattie Poquette,[email protected],283.23995223
Meaghan Garufi,[email protected],227.142916195
Gladys Rim,[email protected],243.459635712
Yuki Whobrey,[email protected],128.321717297
Fletcher Flosi,[email protected],221.394141603

And here's the desired resulting file:
Art Venere    [email protected]    256.62394383
Lenna Paprocki    [email protected]    259.688783099
Donette Foller    [email protected]    282.32979844
Simona Morasca    [email protected]    139.51911647
Dominque Dickerson    [email protected]    167.045197551
Leota Dilliard    [email protected]    253.646335223
Sage Wieser    [email protected]    155.55576823
Kris Cho    [email protected]    210.794277775
Minna Amigon    [email protected]    95.47955397
Abel Maclead    [email protected]    225.774477397
Kiley Caldarera    [email protected]    172.957628871
Graciela Ruta    [email protected]    202.68364784
Josephine Darakjy    [email protected]    178.877840188
Cammy Albares    [email protected]    290.446513401
Mattie Poquette    [email protected]    283.23995223
Meaghan Garufi    [email protected]    227.142916195
Gladys Rim    [email protected]    243.459635712
Yuki Whobrey    [email protected]    128.321717297
Fletcher Flosi    [email protected]    221.394141603

The solution to the exercise is as follows:
awk 'BEGIN{FS=","; OFS="\t"} {print $1, $2, $3}' nameemailavg.csv > output1.txt

However, while doing the exercise I have misunderstood the role of the fields and records separators and my command turned out as follows:
awk 'BEGIN {RS=","; ORS="\t"} {print}' nameemailavg.csv > output2.txt

To my surprise, it worked anyway. The two outputs are the same.

Well, almost. Actually, the second output file gets an additional empty line at the end, but this is not my main concern:
cmp output1.txt output2.txt 
cmp: EOF on output1.txt after byte 921, line 19

Question is, what exactly is going on here? From my understanding, the wrong command makes it so that every single comma-separated field is seen as a record instead. What I can't really figure out is how awk manages to generate the desired output anyway.
$100 Digital Ocean Credit
Get a free VM to test out Linux!

Members online