AWK Command

From rbachwiki
Jump to navigation Jump to search

Print file

awk '{print}' filename.txt

Find text in file

awk '/bug/ {print}' filename.txt

Print certain columns

awk '{print $1,$2}' filename.txt

Print out line numbers

awk '{print NR, $0}' filename.txt
awk '{print $1, $NF}' filename.txt
// $0 prints the entire line
// NR prints the line number
// NF prints the last field of the file
awk '{print NR "- " $1}' filename.txt
// adds a - between columns

AWK using Custom delimiter

Awk -F "," '{print $1}'

AWK using More than one Delimiter

awk -F ",|\" '{print $1}'
using comma and slash as the seperator

AWK bash script to search csv file

echo -e "\nEnter search term"
read varname
# echo -e "\nEnter File Name"
# read fname
fname="export.csv"
# echo -e "\nEnter Delemiter"
dm1=','
dm2='/'
# echo -e "\nDelimeter 2"
# read dm2
cat $fname | grep -i "$varname" | awk -F"$dm1|$dm2" '{print $2, $3, $5, "-", $6}'| sed 's/\"/*/g'

search log file

cat /var/log/apache2/access.log | grep -i 'ads.outwaterads.com' | awk '{print  $1, $2,$3,$4, $6,$7}'