using awk to append numbers to the start of a list
awk '{print NR " " $0}' list > list-numbered
using awk to print a range of lines from a list
awk '$1 == 2 , $1 == 7' list
display non blank and non commented lines in file
filter blank and comment lines from file
cat /etc/ssh/sshd_config | grep -v \# | sed '/^$/d'
cat /etc/ssh/sshd_config | grep -v \# | awk 'NF'
grep -v ^# /etc/samba/smb.conf | grep -v ^\; | awk 'NF'
Use awk to auto generate repeating block of code eg.. creating a really long if else or case statement
I was creating a keyboard mapper program and instead of typing loads of else if statements manually... I create a paired list of the input/match and the output/action and used the awk script below to generate the else if statements.
A 14
B 2F
C 26
D 24
E 22
F 2C
G 2D
H 35
I 3A
J 34
K 3C
L 44
M 36
N 37
O 42
P 4A
Q 12
R 2A
S 1C
T 2B
U 32
V 2E
W 1A
X 1E
Y 33
Z 16
use awk to generate
#the spaces in the following line are required
cat inputed_list | awk '{ print " else if ( c = '"'"'"$1"'"'"' ){\n key_value = "$2";\n single_upper(c);\n }"}'
the output c code
else if ( c = 'T' ){
key_value = 2B;
single_upper(c);
}
else if ( c = 'U' ){
key_value = 32;
single_upper(c);
}
else if ( c = 'V' ){
key_value = 2E;
single_upper(c);
}
else if ( c = 'W' ){
key_value = 1A;
single_upper(c);
}
else if ( c = 'X' ){
key_value = 1E;
single_upper(c);
}
else if ( c = 'Y' ){
key_value = 33;
single_upper(c);
}
else if ( c = 'Z' ){
key_value = 16;
single_upper(c);
Simple script using awk to dump a list of the filesystems that are over 70% full to a file in /tmp
#!/bin/bash
#seamus Dec 2004
out=/tmp/diskusage
name=`uname -n`
date=`date`
echo > $out
echo $name $date >> $out
df -k | awk '{if( $5 > 70 ) print $0 }' | awk '!/Filesystem/' >> $out
cat $out
use sed to append spaces to start of each line for markdown input
cat {file} | sed s/^/\ \ \ \ /g
script to replace the spaces in filenames with underscrores
#!/bin/bash
# this script was created to replace the spaces in filenames with underscrores
#seamus Dec 2004
#if [ $1 != do ] || [ $1 != check ]
#then
# echo USAGE: do or check
# exit
#
#fi
case "$1" in
do)
for i in *
do
mv "$i" `echo -n "$i" | sed 's/\ /_/g'`
done
;;
check)
for i in *
do
echo;echo -n "rename"
echo;echo "$i"
echo to.......
echo -n "$i" | sed 's/\ /_/g'
echo
done
;;
*)
echo USAGE: do or check
esac