Uma solução em awk puro:
awk '
# Read the file and print it with the additional column
function process_file( SIZE, FILE, NEW_FILE ) {
NEW_FILE = FILE ".tmp"
while ( ( getline < FILE ) > 0 ){
print $0 "," SIZE > NEW_FILE
}
close( FILE )
close( NEW_FILE )
system( "mv -f " NEW_FILE " " FILE )
}
# File name has changed, we just passed the end of a file,
# => print the current file
( NR > 1 ) && ( PREV_FILE != FILENAME ) {
process_file( OLD_FNR, PREV_FILE )
}
{
# Save the current file name and line number within the file
PREV_FILE = FILENAME
OLD_FNR = FNR
}
END {
# Print the last file, if any
if ( PREV_FILE != "" ) {
process_file( OLD_FNR, PREV_FILE )
}
}' file1 file2 ...
É muito mais fácil com o gawk , que tem uma condição ENDFILE :
gawk '
# Read the file and print it with the additional column
function process_file( SIZE, FILE, NEW_FILE ){
NEW_FILE = FILE ".tmp"
while ( ( getline < FILE ) > 0 ){
print $0 "," SIZE > NEW_FILE
}
close( FILE )
close( NEW_FILE )
system( "mv -f " NEW_FILE " " FILE )
}
# End of a file, print the file
# FILENAME is the name of the current file being read
# FNR is the line number within the file
ENDFILE {
process_file( FNR, FILENAME )
}' file1 file2 ...