awk

Awk is my favorite CLI program for parsing different input

# awk print out last field using separator “.”

# echo "one.two.three" |awk 'BEGIN{FS="."}{print $NF;}'
three

# awk print out last but one field using separator “.”

# echo "one.two.three" |awk 'BEGIN{FS="."}{X=NF-1; print $X;}'
two

# awk – print fields 1-3 only from matching line using separator “:”. Simplified version using grep below.

# awk 'BEGIN{FS=":";}/root/{print $1 $2 $3;}'< /etc/passwd
rootx0
# grep root /etc/passwd| awk 'BEGIN{FS=":";}{print $1 $2 $3;}'
rootx0

# find all unique file extensions

# find /path/to/files/ -type f |awk 'BEGIN{FS=".";}{print $NF;}' |sort |uniq

If you found this useful, say thanks, click on some banners or donate, I can always use some beer money.

Leave a Reply

Your email address will not be published. Required fields are marked *