我正在尝试将最后一行添加到我正在创建的文件中.如何在END之前检测awk中文件的最后一行?我需要这样做,因为变量在END块中不起作用,
所以我试图避免使用END.
所以我试图避免使用END.
awk ' { do some things..; add a new last line into file;}'
在END之前,我不希望这样:
awk 'END{print "something new" >> "newfile.txt"}'
一种选择是使用getline函数来处理文件.它在成功时返回1,在文件末尾返回0,在出错时返回-1.
awk ' FNR == 1 { ## Process first line. print FNR ": " $0; while ( getline == 1 ) { ## Process from second to last line. print FNR ": " $0; } ## Here all lines have been processed. print "After last line"; } ' infile
假设有这些数据:
one two three four five
输出将是:
1: one 2: two 3: three 4: four 5: five After last line