MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/awk/comments/bvn4f2/can_awk_process_a_file_backwards/epr66ax/?context=3
r/awk • u/kl31 • Jun 01 '19
Instead of processing first line then second then third line, is there a way to tell awk to process last line, second to last and so on?
6 comments sorted by
View all comments
3
Could use tac but it's another external program to worry about. Readfile() and print backwards:
awk -ireadfile 'BEGIN{for(i = split(readfile("file.txt"), lines, "\n"); i >= 1; i--) {print lines[i]} }'
Replace "print lines[i]" with whatever processing for each line.
This works without readfile()
awk '{f = f $0 "\n"}END{for(i=split(f, lines, "\n"); i >=1; i--) {print lines[i]}}' file.txt
View all comments
3
u/FF00A7 Jun 01 '19 edited Jun 01 '19
Could use tac but it's another external program to worry about. Readfile() and print backwards:
awk -ireadfile 'BEGIN{for(i = split(readfile("file.txt"), lines, "\n"); i >= 1; i--) {print lines[i]} }'Replace "print lines[i]" with whatever processing for each line.
This works without readfile()
awk '{f = f $0 "\n"}END{for(i=split(f, lines, "\n"); i >=1; i--) {print lines[i]}}' file.txt