Home > database >  Save print and hash output separately
Save print and hash output separately

Time:01-25

New to the awk and linux environment

Store    Sales    ID     
X        500      A1
X        600      A1
Y        200      A2
Z        300      A3
...

I have a csv file which I am reading $2 column of and saving to a file 'book.csv'. At the same time I am creating a hash file of column $3 and saving it as 'ID.csv'. How can I do that in the same command? Basically combine these two into one.

awk -F '{ print $2 }' raw.csv > book.csv

awk -F '{a[$3]}END{for (x in a) print x}' raw.csv > ID.csv

CodePudding user response:

$ awk '{print $2 > "book.csv"} !seen[$3]  {print $3 > "ID.csv"}' raw.csv

$ cat book.csv 
Sales
500
600
200
300

$ cat ID.csv 
ID
A1
A2
A3

Note that the use of > doesn't mean that the file will get overwritten everytime. That happens only once if the file already existed prior to executing the awk command. Use >> if you wish to append to already existing files.

CodePudding user response:

Dynamically save files based on column name

for i in 2 3; do
  column_name=$(cat raw.csv | awk "{print \$$i}" | head -n 1)
  cat raw.csv | awk "{print \$$i}" | tee $column_name.csv
done

Creates Sales.csv (column 2) ID.csv(column 3)

  •  Tags:  
  • Related