Main Menu

Search

Showing posts with label Linker Article Linux awk. Show all posts
Showing posts with label Linker Article Linux awk. Show all posts

LINUX: AWK Command to Print All Lines In the File Without Duplicate entries/lines

Below AWK Command can be used to Print All Lines In the File Without Duplicate entries/lines.

awk '!A[substr($0, index($0, " "))]++' list.update


For e.g. if you have testfile with below lines, as you can see test-a and test-d lines are repeated with 2 occurrences.

test-a
test-b
test-c
test-a
test-d
test-d

Running above awk command on testfile, will print all lines avoiding printing duplicate line which is already printed. Output will be as follows

test-a
test-b
test-c
test-d

LINUX: Sort Command To Sort Output On Specific Column Numerically In Ascending Order (How To)

For e.g. if you want to sort output from a file in ascending order on specific column numerically, in this e.g. lets assume column 5, below command can be used.

cat filename | sort -n -k5

Replace 5 in above command with whatever column name you want to sort.

LINUX: Sort Command To Sort Output On Specific Column in Descending Order (How To)

For e.g. if you want to sort output from a file in descending order on specific column, in this e.g. lets assume column 5, below command can be used.

cat filename | awk {'print $5'} | sort -n

Replace 5 in above command with whatever column name you want to sort.

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: How To Remove/Ignore/Not Display Blank Lines in A File Using AWK command

Below awk command can be used for removing/ignoring and not displaying blank lines in a file.
awk /./  <filename>

Products to which Article Applies

All Linux Operating Systems

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/