EN
Bash - extract specific column from result
9 points
In this short article, we would like to show how to extract a specific column from some text or program result.
Quick solution:
xxxxxxxxxx
1
cat /path/to/my/file | awk '{print $1}'
2
3
# or
4
5
some-command | awk '{print $1}'
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
).
xxxxxxxxxx
1
$ ls -al | awk 'NR>1 {print $1}'
2
drwxr-xr-x
3
drwxr-xr-x
4
lrwxrwxrwx
5
drwxr-xr-x
6
drwxrwxr-x
7
drwxr-xr-x
8
drwxr-xr-x
9
drwxr-xr-x
10
lrwxrwxrwx
11
lrwxrwxrwx
12
lrwxrwxrwx
13
lrwxrwxrwx
14
drwx------
15
drwxr-xr-x
16
drwxr-xr-x
17
drwxr-xr-x
18
dr-xr-xr-x
19
drwx------
20
drwxr-xr-x
21
lrwxrwxrwx
22
drwxr-xr-x
23
drwxr-xr-x
24
-rw-------
25
dr-xr-xr-x
26
drwxrwxrwt
27
drwxr-xr-x
28
drwxr-xr-x
Direct ls -al
result:
xxxxxxxxxx
1
$ ls -al
2
total 2097248
3
drwxr-xr-x 20 root root 4096 paź 12 13:04 .
4
drwxr-xr-x 20 root root 4096 paź 12 13:04 ..
5
lrwxrwxrwx 1 root root 7 paź 12 13:03 bin -> usr/bin
6
drwxr-xr-x 4 root root 4096 lut 10 10:18 boot
7
drwxrwxr-x 2 root root 4096 paź 12 13:04 cdrom
8
drwxr-xr-x 22 root root 5080 lut 10 09:20 dev
9
drwxr-xr-x 142 root root 12288 lut 10 10:18 etc
10
drwxr-xr-x 3 root root 4096 paź 12 13:06 home
11
lrwxrwxrwx 1 root root 7 paź 12 13:03 lib -> usr/lib
12
lrwxrwxrwx 1 root root 9 paź 12 13:03 lib32 -> usr/lib32
13
lrwxrwxrwx 1 root root 9 paź 12 13:03 lib64 -> usr/lib64
14
lrwxrwxrwx 1 root root 10 paź 12 13:03 libx32 -> usr/libx32
15
drwx------ 2 root root 16384 paź 12 13:03 lost+found
16
drwxr-xr-x 3 root root 4096 paź 12 14:51 media
17
drwxr-xr-x 2 root root 4096 lip 31 2020 mnt
18
drwxr-xr-x 5 root root 4096 sty 19 12:31 opt
19
dr-xr-xr-x 469 root root 0 lut 10 09:19 proc
20
drwx------ 9 root root 4096 sty 22 10:31 root
21
drwxr-xr-x 39 root root 1120 lut 10 10:21 run
22
lrwxrwxrwx 1 root root 8 paź 12 13:03 sbin -> usr/sbin
23
drwxr-xr-x 21 root root 4096 sty 21 09:41 snap
24
drwxr-xr-x 2 root root 4096 lip 31 2020 srv
25
-rw------- 1 root root 2147483648 paź 12 13:03 swapfile
26
dr-xr-xr-x 13 root root 0 lut 10 09:19 sys
27
drwxrwxrwt 33 root root 12288 lut 10 10:23 tmp
28
drwxr-xr-x 14 root root 4096 lip 31 2020 usr
29
drwxr-xr-x 14 root root 4096 lip 31 2020 var