Skip to main content
added 2 characters in body
Source Link
Stéphane Chazelas
  • 586.2k
  • 96
  • 1.1k
  • 1.7k

One caveat though is that it skips empty files (contrary to perl -0777 -n). That can be addressed with GNU awk by putting the code in a ENDFILE statement instead. But we also need to reset $0 in a BEGINFILEBEGINFILE statement as it would otherwise not be reset after processing an empty file:

One caveat though is that it skips empty files (contrary to perl -0777 -n). That can be addressed with GNU awk by putting the code in a ENDFILE statement instead. But we also need to reset $0 in a BEGINFILE statement as it would otherwise not be reset after processing an empty file:

One caveat though is that it skips empty files (contrary to perl -0777 -n). That can be addressed with GNU awk by putting the code in a ENDFILE statement instead. But we also need to reset $0 in a BEGINFILE statement as it would otherwise not be reset after processing an empty file:

added 573 characters in body
Source Link
Stéphane Chazelas
  • 586.2k
  • 96
  • 1.1k
  • 1.7k

To work with several files at a time, one approach would be to do all the file reading by hand in a BEGIN statement (here assuming a POSIX awk, not the /bin/awk of Solaris with the API from the 70s):

awk -- ' BEGIN { for (i = 1; i < ARGC; i++) { FILENAME = ARGV[i] $0 = "" while ((getline line < FILENAME) > 0) $0 = $0 line "\n" # actual processing here, example: print i". "FILENAME" has "NF" fields and "length()" characters." } }' *.txt 

Same caveats about trailing newlines. That one has the advantage of being able to work with filenames that contain = characters.

To work with several files at a time, one approach would be to do all the file reading by hand in a BEGIN statement (here assuming a POSIX awk, not the /bin/awk of Solaris with the API from the 70s):

awk -- ' BEGIN { for (i = 1; i < ARGC; i++) { FILENAME = ARGV[i] $0 = "" while ((getline line < FILENAME) > 0) $0 = $0 line "\n" # actual processing here, example: print i". "FILENAME" has "NF" fields and "length()" characters." } }' *.txt 

Same caveats about trailing newlines. That one has the advantage of being able to work with filenames that contain = characters.

added 6 characters in body
Source Link
Stéphane Chazelas
  • 586.2k
  • 96
  • 1.1k
  • 1.7k

In those, RS is just one character, they don't have BEGINFILEBEGINFILE/ENDFILEENDFILE, they don't have the RTRT variable, they also generally can't process the NUL character.

In those, RS is just one character, they don't have BEGINFILE/ENDFILE, they don't have the RT variable, they also generally can't process the NUL character.

In those, RS is just one character, they don't have BEGINFILE/ENDFILE, they don't have the RT variable, they also generally can't process the NUL character.

added 86 characters in body
Source Link
Stéphane Chazelas
  • 586.2k
  • 96
  • 1.1k
  • 1.7k
Loading
Source Link
Stéphane Chazelas
  • 586.2k
  • 96
  • 1.1k
  • 1.7k
Loading