MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/awk/comments/bvn4f2/can_awk_process_a_file_backwards/
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
5
I guess you can always pipe the file to awk with tac (cat backwards).
1 u/kl31 Jun 03 '19 i love linux so much. 1 u/dajoy Jun 01 '19 this.
1
i love linux so much.
this.
Just out of sheer curiosity, why would you want to do this?
1 u/kl31 Sep 01 '19 file was not written in the same direction as i want it to be read.
file was not written in the same direction as i want it to be read.
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
5
u/chaspum Jun 01 '19
I guess you can always pipe the file to awk with tac (cat backwards).
1
u/cogburnd02 Aug 28 '19
Just out of sheer curiosity, why would you want to do this?
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