0

I'm working on an awk one-liner to substitute commas to tabs in a file ( and swap \\N for missing values in preparation for MySQL select into).

The following link http://www.unix.com/unix-for-dummies-questions-and-answers/211941-awk-output-field-separator.html (at the bottom) suggest the following approach to avoid looping through the variables:

 echo a b c d | awk '{gsub(OFS,";")}1'

 head -n1 flatfile.tab | awk -F $'\t' '{for(j=1;j<=NF;j++){gsub(" +","\\N",$j)}gsub(OFS,",")}1'

Clearly, the trailing 1 (can be a number, char) triggers the printing of the entire record. Could you please explain why this is working?

SO also has Print all Fields with AWK separated by OFS , but in that post it seems unclear why this is working.

Thanks.

Community
  • 1
  • 1
user2105469
  • 1,413
  • 3
  • 20
  • 37

1 Answers1

2

Awk evaluates 1 or any number other than 0 as a true-statement. Since, true statements without the action statements part are equal to { print $0 }. It prints the line.

For example:

$ echo "hello" | awk '1'
hello
$ echo "hello" | awk '0'
$
Community
  • 1
  • 1
jaypal singh
  • 74,723
  • 23
  • 102
  • 147