4

I'm trying to figure this out.

awk '{print $1","$10","$11","$12","$13,$14,$15,$16,$17,$18,$19}' <<< "$PASTE_1" > test.csv

I need to print the $1 $10 $11 $12 separated by comma then continue with $13 until the of the line, without comma separation. Since there are many blank spaces from $13.

1
  • adding a sample input and output would help... – Sundeep Apr 24 '17 at 9:47
7

Do you mean something like this:

awk '{a = ""; for (i = 13 ; i <= NF ; i++) a = a $i; 
       print $1 "," $10 "," $11 "," $12 "," a}'

The input

a b c d e f g h i j k l m n o p q r s t u v w x y z

gives:

a,j,k,l,mnopqrstuvwxyz

That is, the fields starting from 13 are concatenated together, and then printed after 1, 10, 11 and 12.

0
2

One way to do it:

awk -v OFS=, '{print $1, $10, $11, $12, ($13 $14 $15 $16 $17 $18 $19) }'
0
perl -lane 'print join(",", @F[0,9..11], q//), @F[12..$#F]'

Explanation

  1. Array @F holds the fields in a line. @F[...] refers to a slice of elements of array @F. So, @F[0,9..11] is the slice consisting of the 1st, 10th to 12 elements of @F which are then join-ed together by comma and a dummy empty element q// is suffixed to the slice to generate the required , for the coming slice. The other slice @F[12..$#F] gathers the 13th till the last element of the @F array via the $#F which holds the last element's idx. (Please note that array indexing is zero-starting in Perl).
  2. The various arguments to print are now joined together with the OFS = $, (defaults to null).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.