carte blanche
carte blanche

Reputation: 11476

Execute a command in the re-verse order of ids present in a file

I am running the following command using awk on file.txt ,currently its running the command on the ids present in file.txt from top to bottom..i want the commmand to be run in the reverse order for the ids present in file.txt..any inputs on how we can do this?

git command $(awk '{print $1}' file.txt)

file.txt contains.

97a65fd1d1b3b8055edef75e060738fed8b31d3 
fb8df67ceff40b4fc078ced31110d7a42e407f16
 a0631ce8a9a10391ac4dc377cd79d1adf1f3f3e2 

.....

Upvotes: 1

Views: 49

Answers (2)

Atlas Wegman
Atlas Wegman

Reputation: 567

If you aren't bound to using awk then tail with the -r (for reverse) argument will do the trick...

myFile.txt

97a65fd1d1b3b8055edef75e060738fed8b31d3 
fb8df67ceff40b4fc078ced31110d7a42e407f16
a0631ce8a9a10391ac4dc377cd79d1adf1f3f3e2

Now to print it in reverse...

$ tail -r myFile.txt

a0631ce8a9a10391ac4dc377cd79d1adf1f3f3e2
fb8df67ceff40b4fc078ced31110d7a42e407f16
97a65fd1d1b3b8055edef75e060738fed8b31d3 

EDIT: To output this to a file simply redirect it out...

$ tail -r myFile.txt > newFile.txt

EDIT: Want to write to the same file? No problem!

tail -r myFile.txt > temp.txt; cat temp.txt > myFile.txt; rm temp.txt;

For some reason when I redirected tail -r to the same file it came back blank, this workaround avoids that issue by writing to a temporary "buffer" file.

Upvotes: 1

Fredrik Pihl
Fredrik Pihl

Reputation: 45672

To reverse the lines in a file using awk, use

awk '{a[i++]=$0} END {for (j=i-1; j>=0;) print a[j--] }' file

use $1 instead of $0 above to operate on the first field only instead of the whole line.

Upvotes: 1

Related Questions