1

I have 9 column like c1 c2 c3 c4 c5 c6 c7 c8 c9 and I want to cat the value of c1 c2 c3 c4 c5 and c9.

Columns have following data in CSV format. How can I do this in Linux by CLI? Please help

Sample data

123,B006195,T,O,INDIVIDUAL,25^5820200^,2018-04-25,13,NEW
12,C06195,T,O,INDIVIDUAL,25^5820200^,2018-04-25,13,NEW
12345,B00619,T,O,IND,25^5820200^,2018-04-25,13,OLD

I have tried to use cat file.csv | awk '{print $1,$2,$3,$4,$5}' > newfile

Allan
  • 12,117
  • 3
  • 27
  • 51
Deepak Sharma
  • 331
  • 3
  • 11
  • 1
    Do you want to keep only the values of c1 c2 c3 c4 c5 and c9? and trash the other columns right? – Allan May 10 '18 at 08:38
  • Possible duplicate of [Extract specific columns from delimited file using Awk](https://stackoverflow.com/q/7857090/608639), [How to print a range of columns in a CSV in AWK?](https://stackoverflow.com/q/25461806/608639), [How to extract one column of a csv file](https://stackoverflow.com/q/19602181/608639), [Printing column separated by comma using command line](https://stackoverflow.com/q/26842504/608639), etc. – jww May 10 '18 at 09:55
  • 1
    DOWN VOTER, please provide reason for down votes on answers. – RavinderSingh13 May 10 '18 at 10:11
  • If you found other answers helpful, please up-vote them. Accepting and up-voting helps future readers. – Allan May 11 '18 at 05:58

2 Answers2

1

I am not sure about what you mean by cat the value of c1 c2 c3 c4 c5 and c9 however if want you want is to filter only those columns, then you can use the following awk command:

awk 'BEGIN{OFS=FS=","}{print $1,$2,$3,$4,$5,$9}' sample.csv

INPUT:

more sample.csv 
c1,c2,c3,c4,c5,c6,c7,c8,c9
123,B006195,T,O,INDIVIDUAL,25^5820200^,2018-04-25,13,NEW
12,C06195,T,O,INDIVIDUAL,25^5820200^,2018-04-25,13,NEW
12345,B00619,T,O,IND,25^5820200^,2018-04-25,13,OLD

OUTPUT:

awk 'BEGIN{OFS=FS=","}{print $1,$2,$3,$4,$5,$9}' sample.csv 
c1,c2,c3,c4,c5,c9
123,B006195,T,O,INDIVIDUAL,NEW
12,C06195,T,O,INDIVIDUAL,NEW
12345,B00619,T,O,IND,OLD

Explanations:

You define as field separator (input,output) a , (BEGIN{OFS=FS=","}) then you just print for each line the columns that you need to display {print $1,$2,$3,$4,$5,$9} after you redirect the output to a new csv file

If you think that awk is an overkill for this task than you can also just use the cut command (-d',' is to define a , as delimiter and -f... is to specify which field need to be kept):

$ cut -d',' -f1,2,3,4,5,9 sample.csv
c1,c2,c3,c4,c5,c9
123,B006195,T,O,INDIVIDUAL,NEW
12,C06195,T,O,INDIVIDUAL,NEW
12345,B00619,T,O,IND,OLD
Allan
  • 12,117
  • 3
  • 27
  • 51
1

Following solution may help you on same, you need to provide field numbers in variable of awk named fields and could print it.

awk -F, -v fields="1,2,3,4,5,9" 'BEGIN{num=split(fields, array,",")} {for(i=1;i<=num;i++){printf("%s%s",$array[i],i==num?ORS:OFS)}}' OFS=,   Input_file

Adding a non-one liner form of solution too now.

awk -F, -v fields="1,2,3,4,5,9" '
BEGIN{
  num=split(fields, array,",")}
{
  for(i=1;i<=num;i++){
    printf("%s%s",$array[i],i==num?ORS:OFS)}}
' OFS=,   Input_file

Explanation of above code:

awk -F, -v fields="1,2,3,4,5,9" '              ##Setting field seprator as comma here with -F. Setting variable named fields with values of fields which we need.
BEGIN{                                         ##Starting BEGIN section here for awk which will be executed before reading the Input_file.
  num=split(fields, array,",")}                ##using split to split the variable fields into array named array and creating variable num which will have number of element of array.
{
  for(i=1;i<=num;i++){                         ##Starting a for loop here which starts from variable named i value from 1 to till value of variable num.
    printf("%s%s",$array[i],i==num?ORS:OFS)}}  ##Printing value of array[i] and then $array[i] will print the field value in current line too. Then checking condition variable i value equal to variable num then print new line else print space with OFS.
' OFS=,  Input_file                            ##Mentioning the Input_file name here.
RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93