Combining two awk teams in one team

I want to combine these two teams and I want to call one command

In the first command, I save the 4th column of the x.csv file (Separator,) in the z.csv file.

awk -F, '{print $4}' x.CSV > z.csv

In the second command, I want to find out the unique value of the first column of the z.csv file (Separator-space).

awk -F\  '{print $1}' z.csv|sort|uniq

I want to combine these two teams in a single team. How can i do this?

+4
source share
3 answers

Connect the output of the first awkto the second awk:

awk -F, '{print $4}' x.CSV | awk -F\  '{print $1}' |sort|uniq

or, as Avinash Raj suggested,

awk -F, '{print $4}' x.CSV | awk -F\  '{print $1}' | sort -u
+4
source

Assuming content is z.csvreally needed, and not just an artifact of how you are currently implementing your program, you can use:

awk -F, '{ print $4 > "z.csv"
           split($4, f, " ")
           f4[f[1]] = 1
         }
         END { for (i in f4) print i }' x.CSV

split 4 , () f4 . , . , GNU awk ( awk ), awk sort.

GNU awk END :

         END { asorti(f4); for (i in f4) print f4[i] }

z.csv, (a) , (b) print $4 > "z.csv".

+5
awk '{split($4,b," "); a[b[1]]=1} END { for( i in a) print i }' FS=, x.CSV 

, , , . , , sort.

+4

Source: https://habr.com/ru/post/1545935/


All Articles