add the old snippet md files
This commit is contained in:
parent
fc0dd204c7
commit
bcf8313a4b
110 changed files with 3048 additions and 0 deletions
34
old_snippets/awk-last-column.en.md
Normal file
34
old_snippets/awk-last-column.en.md
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
---
|
||||
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/)
|
||||
Loading…
Add table
Add a link
Reference in a new issue