Main Menu

Search

Showing posts with label print. Show all posts
Showing posts with label print. Show all posts

LINUX: AWK Command To Print Particular Column/Field From Last In Command Output Or File (How To Doc)

Below awk command can be used to print particular column/field from last in command output or file.
cat <filename> | awk '{print $(NF-<column number from last>)}'
<command> | awk '{print $(NF-<column number from last>)}'

To demonstrate this with an example, if you have file test.out with below content and 4 columns.

A789652=alpha   22      33      44
A789651=alpha   AA      BB      CC
A28ee69=beta    XX      YY      ZZ
A28ee6a=beta    11      99      88
A2991a5=alpha   12      34      56

If you want to print second column/field from last in test.out file, your awk command looks as follows:
# cat test.out | awk '{print $(NF-2)}'

Below is example output of above command. As you can see it printed 2nd column.

33
BB
YY
99
34

Products to which Article Applies

All Linux Operating Systems

Additional Reference

http://linuxcommand.org/lc3_adv_awk.php

LINUX: AWK Command To Print Particular Column In Command Output Or File (How To Doc)

Below awk command can be used to print particular column in command output or file.
cat <filename> | awk {'print $<column number>'}
<command> | awk {'print $<column number>'}

To demonstrate this with an example, if you have file test.out with below content and 4 columns.

A789652=alpha   22      33      44
A789651=alpha   AA      BB      CC
A28ee69=beta    XX      YY      ZZ
A28ee6a=beta    11      99      88
A2991a5=alpha   12      34      56

If you want to print second column in test.out file, your awk command looks as follows:

# cat test.out | awk {'print $2'}

Below is example output of above command. As you can see it printed 2nd column.

22
AA
XX
11
12



Products to which Article Applies

All Linux Operating Systems

Additional Reference

http://linuxcommand.org/lc3_adv_awk.php

LINUX: SED Command To Print Lines In A File Between Two Matching Patterns (How To Doc)

Below SED Command can be used to pring lines in a file between two matching patterns.
sed -n '/<string1>/,/<string2>/p' <filename>

To demonstrate this with example, lets assume we have testfile.out with below contents.

A789652=alpha
A789651=alpha
A28ee69=beta
A28ee6a=beta
A2991a5=alpha
A2991a6=alpha
A3285c1=alpha
A3285c2=alpha
A32860d=alpha
A32860e=alpha
A3286d5=alpha

If you want to print lines between "A28ee6a" and "A3285c1" in testfile.out file, your SED command looks as follows:
sed -n '/A28ee6a/,/A3285c1/p' testfile.out

Below is example output of above command, As you can see it printed 4 lines only.

# sed -n '/A28ee6a/,/A3285c1/p' testfile.out
A28ee6a=beta
A2991a5=alpha
A2991a6=alpha
A3285c1=alpha


Products to which Article Applies

All Linux Operating Systems

Additional Reference

https://likegeeks.com/sed-linux/

LINUX: Awk Command To Print Particular Column Or Columns From Output(How To Doc)

Awk command can be used to filter output and display particular column or columns in the output.
For e.g. if you want to print only print particular column from "ls -lrt" command below command can be used.

ls -lrt | awk {'print $n'}

In above command replace n with the column number you want to print.

For e.g. if you want to print two or more columns from ls -lrt output below command can be used.

ls -lrt | awk {'print $n1,$n2'}

In above command replace n1 and n2 with the column numbers you want to print.

In above example we use awk with "ls -lrt", but awk command can be used with any commands like cat, tar etc.

To demonstrate above command with output, for e.g. if "ls -lrt" command output is shown as follows:


drwxr-xr-x.   2 root root    6 Aug 30  2016 srv
drwxr-xr-x.   2 root root    6 Aug 30  2016 opt
drwxr-xr-x.   2 root root    6 Aug 30  2016 mnt
drwxr-xr-x.   2 root root    6 Aug 30  2016 media
drwxr-xr-x.   3 root root   16 Aug 30  2016 home

If you run below awk command to print only column 9
ls -lrt | awk {'print $9'}

Your output will only list column 9 as follows:
srv
opt
mnt
media
home

If you run below awk command to pring only columns 1 and 9



ls -lrt | awk {'print $1,$9'}

your output will look as follows:

drwxr-xr-x. srv
drwxr-xr-x. opt
drwxr-xr-x. mnt
drwxr-xr-x. media
drwxr-xr-x. home


Products to which Article Applies


All Linux environments

Additional References

https://www.geeksforgeeks.org/awk-command-unixlinux-examples/