awk last column - 02-13-22

My first awk snippet! Today I needed to get all the file extensions in directory for a blog post I’m writing.

I solved it with:

$ fd . --type f | awk -F"." '{print $(NF)}' |  tr  '[:upper:]' '[:lower:]' | sort | uniq | pbcopy

Break down

fd . –type f, lists all the files in a directory recursively.

awk -F"." ‘{print $(NF)}’, the -F"." tells awk to split columns on “.”. The ’{print $(NF)’} tells awk to print the last column. Normally you do something like ’{print $2}’ to print the second column.

tr ‘[:upper:]’ ‘[:lower:]’, tr is a Unix until to translate characters. In this case all upper case letters will be translated to lower case. I’ve created a seprate snippet for it as well.

sort | uniq, a classic combo sorts the results then gets rid of duplicates.

pbcopy, anther common one for me pipes the result into the clipboard.

source

\- [ awk ]