Last active
June 12, 2025 22:02
-
-
Save henri/18bdeaf815e3c95d21190b528d995cd9 to your computer and use it in GitHub Desktop.
AWK Cheatsheet
This file contains hidden or 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
# How to just buffer each line (rather than the entire input stream) when using awk / gawk to read data from a pipe. | |
awk '{ print $0 ; system("") }' # compatible with POSIX complient systems | |
gawk '{ print $0 ; system("") }' # gawk is smart and will not actually start a sub-shell | |
# fflush - is going to offer something similar (although you can speicify which kind of output you would like to flush). | |
# remove first column (using white space) keep all other columns | |
awk '{sub(/^[^[:space:]]+[[:space:]]+/, ""); print}' |
This file contains hidden or 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
# This script inserts contents of replace.txt after the first occurrence of "<REPLACE_/AFTER/_ME>" | |
/<REPLACE_\/AFTER\/_ME>/ && !found { | |
found = 1 | |
print $0 | |
# Insert contents of replace.txt after the first match | |
while ((getline line < "replace.txt") > 0) { | |
print line | |
} | |
} | |
# Print the current line if it does not match the target string | |
!/<REPLACE_\/AFTER\/_ME>/ { print $0 } | |
This file contains hidden or 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/bin/env bash | |
# this script uses the awk script 500.insert_after_first.awk | |
# to add the data from replace.txt into the files feed into the | |
# while loop. | |
while IFS= read -r current_file ; do | |
echo " processing : $current_file" | |
temp_file=$(mktemp ./mytmp.XXXXXXXX) | |
awk -f add.awk $current_file > $temp_file | |
if [[ $? != 0 ]] ; then | |
echo "ERROR! AWK failed aborting.." | |
exit -99 | |
fi | |
mv "$temp_file" $current_file | |
if [[ $? != 0 ]] ; then | |
echo "ERROR! moving failed aborting.." | |
exit -99 | |
fi | |
done <<< $(ls /path/to/input_data_to_update/*/*/*.txt) |
This file contains hidden or 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
# insert every 5 lines hostname weather a service (systemd) nginx is active. | |
tail -f /path/to/my/file.log | awk 'NR % 5 == 0 {system(" echo -n $(hostname) ; echo -n \" : nginx status : \" ; systemctl is-enabled nginx.service") } {print}' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment