Languages
[Edit]
EN

Bash - extract specific column from result

9 points
Created by:
JustMike
29910

In this short article, we would like to show how to extract a specific column from some text or program result.

Quick solution:

cat /path/to/my/file | awk '{print $1}'

# or

some-command | awk '{print $1}'

Practical example

In the below example, awk command extracts the first column ($1) from ls -al command result, without taking the first row into account (NR>1).

$ ls -al | awk 'NR>1 {print $1}'
drwxr-xr-x
drwxr-xr-x
lrwxrwxrwx
drwxr-xr-x
drwxrwxr-x
drwxr-xr-x
drwxr-xr-x
drwxr-xr-x
lrwxrwxrwx
lrwxrwxrwx
lrwxrwxrwx
lrwxrwxrwx
drwx------
drwxr-xr-x
drwxr-xr-x
drwxr-xr-x
dr-xr-xr-x
drwx------
drwxr-xr-x
lrwxrwxrwx
drwxr-xr-x
drwxr-xr-x
-rw-------
dr-xr-xr-x
drwxrwxrwt
drwxr-xr-x
drwxr-xr-x

Direct ls -al result:

$ ls -al
total 2097248
drwxr-xr-x  20 root root       4096 paź 12 13:04 .
drwxr-xr-x  20 root root       4096 paź 12 13:04 ..
lrwxrwxrwx   1 root root          7 paź 12 13:03 bin -> usr/bin
drwxr-xr-x   4 root root       4096 lut 10 10:18 boot
drwxrwxr-x   2 root root       4096 paź 12 13:04 cdrom
drwxr-xr-x  22 root root       5080 lut 10 09:20 dev
drwxr-xr-x 142 root root      12288 lut 10 10:18 etc
drwxr-xr-x   3 root root       4096 paź 12 13:06 home
lrwxrwxrwx   1 root root          7 paź 12 13:03 lib -> usr/lib
lrwxrwxrwx   1 root root          9 paź 12 13:03 lib32 -> usr/lib32
lrwxrwxrwx   1 root root          9 paź 12 13:03 lib64 -> usr/lib64
lrwxrwxrwx   1 root root         10 paź 12 13:03 libx32 -> usr/libx32
drwx------   2 root root      16384 paź 12 13:03 lost+found
drwxr-xr-x   3 root root       4096 paź 12 14:51 media
drwxr-xr-x   2 root root       4096 lip 31  2020 mnt
drwxr-xr-x   5 root root       4096 sty 19 12:31 opt
dr-xr-xr-x 469 root root          0 lut 10 09:19 proc
drwx------   9 root root       4096 sty 22 10:31 root
drwxr-xr-x  39 root root       1120 lut 10 10:21 run
lrwxrwxrwx   1 root root          8 paź 12 13:03 sbin -> usr/sbin
drwxr-xr-x  21 root root       4096 sty 21 09:41 snap
drwxr-xr-x   2 root root       4096 lip 31  2020 srv
-rw-------   1 root root 2147483648 paź 12 13:03 swapfile
dr-xr-xr-x  13 root root          0 lut 10 09:19 sys
drwxrwxrwt  33 root root      12288 lut 10 10:23 tmp
drwxr-xr-x  14 root root       4096 lip 31  2020 usr
drwxr-xr-x  14 root root       4096 lip 31  2020 var

Bash

Bash - extract specific column from result
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join