Why doesn't OFS work?

why johnny doesn't flap pdf
why johnny doesn't flap read aloud
$ echo "a b c" | awk 'BEGIN {OFS=","}; {print $0};' -
a b c

I was trying to see if OFS applies after the last field, so expecting the output to be either

a,b,c

or

a,b,c,

but the change of OFS doesn't work. Why is it?

You should change/set a field, so that the $0 is recomputed, then OFS will be applied. E.g.

echo "a b c" | awk 'BEGIN {OFS=","}; {$1=$1;print $0};'

Biden Fumbles Interview: Why Doesn't Trump Act Like A President , It's no surprise that President Donald Trump says he won't follow CDC recommendations to wear a mask to stop the spread of coronavirus,  Question: "Why doesn’t God respond when I cry out, ‘God, please help me!’?" Answer: Many have felt abandoned by God in their times of greatest need, asking God for help but not receiving what was asked for. Does this mean that God doesn’t care or that He is ignoring our cries for help?

{ print $1, $2, $3 } will use the OFS value regardless of any fields got updated or not. But this solution is not portable. I am hoping other better solutions such as print $WITH_OFS that may be a new feature of AWK.

Why Trump doesn't want to wear a mask - CNN, Why doesn't Trump? A precinct deputy in St. Petersburg, Fla., wears gloves to assist voters dropping off ballots during Florida's primary election  Cats may purr while they demonstrate aggression — you may have seen your cat purr even as his tail puffs up to about three times its normal size. Cats also purr when they are sick, injured or scared. It’s been suggested that the purr has healing powers, and there may well be some truth to that. Not Every Cat Purrs. So why doesn’t your cat

$0 is not modified by assigning to OFS. $0, however, gets modified when you assign to any of its elements, including any non-existing fields.

echo "a b c" | awk 'BEGIN {OFS=","}; {$4="";print $0};' -

gives: a,b,c,

A president should want all Americans to vote. Why doesn't Trump , Why Johnny Doesn't Flap: NT is OK! [Morton, Clay, Morton, Gail, Merry, Alex] on Amazon.com. *FREE* shipping on qualifying offers. Why Johnny Doesn't Flap:  Here's Why Donald Trump Doesn't Want Anyone To Know His Grades Or SAT Scores Christopher Rim Senior Contributor Opinions expressed by Forbes Contributors are their own.

Why Johnny Doesn't Flap: NT is OK!: Morton, Clay, Morton, Gail , Why Doesn't Mark Zuckerberg Get It? The Facebook co-founder's speech at Georgetown was a profile in cowardice. Timothy Egan. By Timothy  Why doesn’t the U.S. use the metric system? Phil Lewis/shutterstock As of today, the entire world has adopted the metric system, with the exception of the United States, Myanmar, and Liberia

Why doesn't the Leaning Tower of Pisa fall over?, Why Doesn't Flu Tank Economy Like Covid-19? Comparisons between two diseases--based on infections and deaths--missed range of  5 Reasons Why Your Bread Dough Doesn't Rise sometimes flour + water + yeast doesn’t equal a tantalizing loaf of bread, but is just a large lump of dough. That's why we want you to save

Alex Gendler: Why doesn't the Leaning Tower of Pisa fall over , Why Doesn't Capital Flow from Rich to Poor Countries? Author(s): Robert E. Lucas, Jr. Source: The American Economic Review, Vol. 80, No. 2, Papers and  But why doesn't God just do something in such a blatant way that people would HAVE to believe he exists? A good answer is presented by Philip Yancey in his book, The Jesus I Never Knew. Yancey points out that God has given us the freedom to believe in him or not.

Comments
  • Thanks. What constructs do you mean that I don't understand in this post? I have the book in pdf file, and on what page can I find them? I am reading The AWK Programming Language by Aho et al, right now.
  • ok. But what constructs do you mean that I don't understand here?
  • OFS, print, field assignment, $0, awk syntax (those semi-colons are neither necessary nor desirable - just cluttering up your code), and how to call awk (you do not need to list - as a file name when reading from a pipe).
  • I would say that this is a good question. Even you read the book backward, this trap will still get you. Awk should provide a different way to print the output with the new value of OFS. Is there an alternative to $0? I know if you do { print $1, $2, $3 } the OFS will be used even you did not update any fields. However, this requires you know the exact number of columns in the data and it is not portable to a different input.
  • Thanks. (1) Why does it work that way? (2) Is it similar for other builtin variables, such as ORS, ....? or is it unique for OFS?
  • only assigning value to a field causing $0 to be recomputed. sub(), gsub() on $0 won't. If there is no change on fields, when you print $0, awk take the whole line, not NF fields. same for sub/gsub I think one reason of this is performance.@Tim
  • @Tim (2) No, ORS will be applied in all cases. because it looks row, not fields. When you print a row the ORS comes with anyway.
  • @MichaelBack no, the idiomatic awk would be echo "a b c" | awk -v OFS=, '{$1=$1}1' so the line is printed even if it's first field is the null string or evaluates to zero. Try both approaches if the first field is 0 instead of a.
  • Yup Ed -- mine's buggy.