RVSP
1
I have requirement to split below file (sample.csv) into multiple files by using the unique columns (first 3 are unique columns)
sample.csv
123|22|56789|ABCDEF|12AB34|2019-07-10|2019-07-10|443.3400|1|1
123|12|5679|BCDEFG|34CD56|2019-07-10|2019-07-10|896.7200|1|2
123|12|5679|CDEFGH|45DE67|2019-07-10|2019-07-10|680.9200|1|1
123|22|56789|DEFGHI|56EF78|2019-07-10|2019-07-10|405.8800|1|2
required output files should be as below
sample_123_22_56789.csv
123|22|56789|ABCDEF|12AB34|2019-07-10|2019-07-10|443.3400|1|1
123|22|56789|DEFGHI|56EF78|2019-07-10|2019-07-10|405.8800|1|2
sample_123_12_5679.csv
123|12|5679|BCDEFG|34CD56|2019-07-10|2019-07-10|896.7200|1|2
123|12|5679|CDEFGH|45DE67|2019-07-10|2019-07-10|680.9200|1|1
I am using below command and it is working but want to make it generic instead of hard coding the $4"|"$5"|"$6"|"$7"|"$8"|"$9"|"$10
awk -F\| '{print $4"|"$5"|"$6"|"$7"|"$8"|"$9"|"$10 > "sample_"$1"_"$2"_"$3".CSV"}' sample.csv
Thanks.
Did you search this forum? There might be an answer already.
Here is a simple one:
awk -F"|" ' { print > ("sample_" $1 "_" $2 "_" $3 ".csv") }' sample.csv
RVSP
3
Thank you for the reply, Sorry for the confusion, my desired output would be as below (Excludeing first 3 columns)
sample_123_22_56789.csv
ABCDEF|12AB34|2019-07-10|2019-07-10|443.3400|1|1
DEFGHI|56EF78|2019-07-10|2019-07-10|405.8800|1|2
sample_123_12_5679.csv
BCDEFG|34CD56|2019-07-10|2019-07-10|896.7200|1|2
CDEFGH|45DE67|2019-07-10|2019-07-10|680.9200|1|1
Ah, now I understand
It's a challenge to split and print in awk.
Here is a RE
awk -F"|" '{ x=$0; sub(/[^|]*[|][^|]*[|][^|]*[|]/, "", x); print x > ("sample_" $1 "_" $2 "_" $3 ".csv") }' sample.csv
Not elegant.
The following in a loop composes the strings for the output and the file name:
awk -F"|" ' { fn="sample"; out=sep=""; for(i=1; i<=NF; i++) if (i<=3) { fn=(fn "_" $i) } else { out=(out sep $i); sep=FS } print out > (fn ".csv") }' sample.csv
1 Like