Created
May 31, 2014 10:19
-
-
Save babarot/38346c78cf4779079a4b to your computer and use it in GitHub Desktop.
Behave like the GNU tair -r command or tac command
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/local/bin/gawk -f | |
BEGIN { | |
# Set delimiter as \034, avoid conflict with other characters | |
sort_exe = "sort -t \"\034\" -nr" | |
} | |
{ | |
# Pass to the child process | |
printf("%d\034%s\n", NR, $0) |& sort_exe; | |
} | |
END { | |
# Close the input pipe only | |
close(sort_exe, "to"); | |
# Read output of the child process by using getline | |
while ((sort_exe |& getline var) > 0) { | |
split(var, arr, /\034/); | |
print arr[2]; | |
} | |
close(sort_exe); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment