Home > Mobile >  How to print column1,column3 and column 9 to column12 of the csv using AWK command?
How to print column1,column3 and column 9 to column12 of the csv using AWK command?

Time:10-05

This below command prints 9-12 column of the csv files perfectly

grep -i introd *.csv | awk 'BEGIN { first = 9; last = 12 }{ for (i = first; i < last; i  ) {printf("%s ", $i) } print $last }

but I also want to print the column1, column3 and column 30-35.

CodePudding user response:

With a small number of fields, it's easy to just do them explicitly, something like:

{ print $1","$3","$9","$10","$11","$12","$30","$31","$32","$33","$34","$35 }

If there are a larger number of fields, you can always create a list of indexes, including with ranges:

BEGIN {
    sz = 0
    split(COLS, ranges, ",")
    for (range in ranges) {
        split(ranges[range], values, "-")
        if (length(values) == 1) {
            idxs[sz  ] = values[1]
        } else {
            for (value = values[1]; value <= values[2]; value  ) {
                idxs[sz  ] = value
            }
        }
    }
    sep = ""
}
{
    for (idx in idxs) {
        printf "%s%s", sep, $idxs[idx]
        sep = ","
    }
    printf "\n"
}

This output shows how it works:

pax> echo a b c d e f g h i j | awk -vCOLS=1-5,3-4,7 -f myprog.awk
a,b,c,d,e,c,d,g

CodePudding user response:

Here is a Generic solution, where you could pass a number of Ranges separated by , and range values should be separated by - within themselves. Also in case you want to pass multiple .csv file yes you can pass all of them like: *.csv to this program too.

awk -v range="9-12,30-35" '
BEGIN{
  FS=OFS=","
  num=split(range,arr,",")
}
{
  delete arr1
  value=""
  for(i=1;i<=num;i  ){
    split(arr[i],arr1,"-")
    for(start=arr1[1];start<=arr1[2];start  ){
       value=(value?value OFS:"")$start
    }
  }
  print value
}
'  Input_file

Explanation: Adding detailed explanation for above.

awk -v range="9-12,30-35" '         ##Starting awk program from here and setting range variable which has different ranges, in this case 9 to 12 AND 30 to 35 field numbers here.
BEGIN{                              ##Starting BEGIN section of this program from here.
  FS=OFS=","                        ##Setting FS and OFS as comma here.
  num=split(range,arr,",")          ##Splitting range to arr array with separator of comma here.
}
{
  delete arr1                       ##Deleting arr1 for safer side.
  value=""                          ##Nullifying value here.
  for(i=1;i<=num;i  ){              ##Running for loop till value of num here.
    split(arr[i],arr1,"-")          ##Splitting arr[i[ value into arr1 here with separator of - here.
    for(start=arr1[1];start<=arr1[2];start  ){ ##Running for loop from arr[1] value to arr1[2] value.
       value=(value?value OFS:"")$start  ##Creating value which has fields value in it.
    }
  }
  print value                       ##printing value here.
}
'  Input_file                       ##Mentioning Input_file name here.

CodePudding user response:

grep -i introd *.csv | awk 'BEGIN { first = 9; last = 12 }{ print $1, $3 for (i = first; i < last; i  ) {printf("%s ", $i) } print $last
  • Related