Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to cut CSV file [duplicate]

Tags:

linux

bash

sed

awk

cut

I have the following CSV file

 more file.csv

 Number,machine_type,OS,Version,Mem,CPU,HW,Volatge

 1,HG652,linux,23.12,256,III,LOP90,220
 2,HG652,linux,23.12,256,III,LOP90,220
 3,HG652,SCO,MK906G,526,1G,LW1005,220
 4,HG652,solaris,1172,1024,2Core,netra,220
 5,HG652,solaris,1172,1024,2Core,netra,220

Please advice how to cut CSV file ( by cut or sed or awk command )

in order to get a partial CSV file

  • Command need to get value that represent the fields that we want to cut from the CSV

    According to example 1 ( value should be 6 )

Example 1

on this example we cut the 6 fields from left to right , ( in this case CSV will look like this )

  Number,machine_type,OS,Version,Mem,CPU

  1,HG652,linux,23.12,256,III
  2,HG652,linux,23.12,256,III
  3,HG652,SCO,MK906G,526,1G
  4,HG652,solaris,1172,1024,2Core
  5,HG652,solaris,1172,1024,2Core
like image 468
maihabunash Avatar asked Oct 21 '25 04:10

maihabunash


2 Answers

cut is your friend:

$ cut -d',' -f-6 file
Number,machine_type,OS,Version,Mem,CPU

1,HG652,linux,23.12,256,III
2,HG652,linux,23.12,256,III
3,HG652,SCO,MK906G,526,1G
4,HG652,solaris,1172,1024,2Core
5,HG652,solaris,1172,1024,2Core

Explanation

  • -d',' set comma as field separator
  • -f-6 print up to the field number 6 based on that delimiter. It is equivalent to -f1-6, as 1 is default.

Also awk can make it, if necessary:

$ awk -v FS="," 'NF{for (i=1;i<=6;i++) printf "%s%s", $i, (i==6?RS:FS)}' file
Number,machine_type,OS,Version,Mem,CPU
1,HG652,linux,23.12,256,III
2,HG652,linux,23.12,256,III
3,HG652,SCO,MK906G,526,1G
4,HG652,solaris,1172,1024,2Core
5,HG652,solaris,1172,1024,2Core
like image 56
fedorqui 'SO stop harming' Avatar answered Oct 23 '25 18:10

fedorqui 'SO stop harming'


the cut commandline is rather simple and well suited in your case:

cut -d, -f1-6 yourfile

So everybody agrees to say that the cut way is the best way to go in this case. But we can also talk about the awk solution, and there I may point out that in fedorqui's answer, a clever trick is used to silence empty lines (NF as a selection pattern), but it has the disadvantage of e.g. removing blank lines from the original file. I propose below another solution (en passant, using the -F option instead of the variable passing mechanism on FS that preserves any empty line and also respects lines with less than 6 fields, e.g. prints these lines without adding extra commas there:

awk -F, '{min=(NF>6?6:NF); for (i=1;i<=min-1;i++) printf "%s,", $i; printf "%s\n", $6}' yourfile

This works nicely because printf-ing $6 is never an error, even in case the line has less than 6 fields. This is true with my gawk 4.0.1, at least...

like image 31
jaybee Avatar answered Oct 23 '25 18:10

jaybee



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!