snippets/old_snippets/awk-last-column.en.md

34 lines
1.2 KiB
Markdown

---
title: "awk last column"
seo_description: "Shell snippet explaining how to extract the last column using awk"
date: 2022-02-13T10:46:08+01:00
draft: false
snippet_types:
- awk
---
My first [awk](https://en.wikipedia.org/wiki/AWK) snippet! Today I needed to get
all the file extensions in directory for a blog post I'm writing.
I solved it with:
```shell
$ 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](/snippets/lower-case) 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](https://linuxhint.com/awk_print_last_column_file/)