Openunix.eu
Awk
- awk - print collumns 1 and 3
cat /etc/passwd | awk '{ print $1 $3 }'
- awk - print line 22
cat /etc/passwd |awk 'NR==22'
- awk - print lines 1 and 3
cat /etc/passwd |awk 'NR==1 || NR==3'
- awk - print lines 1-3
cat /etc/passwd |awk 'NR>=1&&NR<=3'
- awk - print lines 1-3 and 32-36
cat /etc/passwd |awk 'NR>=1&&NR<=3 || NR>=32&&NR<=36'
- awk - the options can be combined
cat /etc/passwd |awk -F: 'NR>=1&&NR<=3 || NR>=3&&NR<=5 { print $1 }'
- awk - print line where it finds 'Jethro'
cat /etc/passwd |awk '/Jethro/'
- awk - another combination
cat /etc/passwd |awk -F: '/ae/ { print $1 } '
- awk - count all numbers in the file
awk '{ sum+=$1} END {print sum}' file_with_numbers.txt
- awk - parse a large log file
find lines with POST and split to files by expressions in third column
replace all "/" in names by "_" before working with the expressions
the "close" is here to prevent "too many files open" error
awk '/POST/' bigfile.log | awk '{gsub("/","_",$3); print $10>> $3; close($3) }'
- awk - print line if 2nd column begins in "VZ" or "VU"
awk '$3 ~ /^VZ|^VU/ {print $1" "$2" " $3}'
- awk - change CAPITALS to small font
awk '{$1=toupper($1);print $0}' file_to_convert.txt
- awk - inheriting bash variables and combining them with awk variables
awk -v r=$remote -v l=$local -v n=$name -v z=$zero '{print "The name " n " from "$1" brings the "$2" to "r >l}'
- awk - searching for special regexp chars - needs a doubble-back-slash
awk -F: '{gsub("\\[", "");gsub("\\/", " ");gsub("nov","11"); print $1" "$2}'
- awk - if condition + using a variable introduced by "-v"
awk -v p="output_file" '{ if($2 ~/^-/) { gsub("^-2", "2", $2 ); print "0 "$2"-" $1 >p} else{print >p} }' input_file
- awk + paste ... split and complete a file
awk -F\; '{print $1}' original_file.txt |tr '[:upper:]' '[:lower:]' >up2low_collumn1.txt
awk -F\; '{print $2}' original_file.txt >collumn2.txt
paste -d ";" $up2lowfinal $title_name >complete_again.txt
Powered by NetBSD. Running on a toaster.