Linux’s AWK

Linux: Adding Numbers in a File

Here’s a Linux command to add numbers inside a file:
[root@myserver misc]# cat num.test
1
2
3
4
5

This is a sample file with only 5 lines. Think of a file with millions of lines. Either you can do with MS – Excel, which may hang(!!) or use Linux command – ‘awk’- which is powerful and easier:

[root@myserver misc]# awk ‘{s+=$1} END {print s}’ num.test
15

Linux: ‘awk’ Command – Group By !

While its pretty easy to do ‘Group By’ at database level, ‘awk’ enables us to do same at file level.
Consider the below file:
# cat test.csv
aa 1 qwer
ab 2 tyui
aa 3 poiu
ab 2 mnb
bb 1 njio
ba 2 njtwe
test.csvĀ  is a tab separated file with 3 columns. Here, I want to segregate the whole lines with matching 1st and 2nd columns into separate files.
Like below:
# cat file_bb_1.csv
bb 1 njio
# cat file_ba_2.csv
ba 2 njtwe
# cat file_ab_2.csv
ab 2 tyui
ab 2 mnb
# cat file_aa_3.csv
aa 3 poiu
# cat file_aa_1.csv
aa 1 qwer
Though you can do this manually, think of a file with more than million lines.
Here, ‘awk’, being an powerful data manipulation tool,comes to our help.
Below is the command we can use:

#cat test.csv | awk ‘{a=$1;b=$2; print $0 >> “file_” a “_” b “.csv”}’

[You can give any name instead of ‘file_’]

 

 

  • Ask Question