Below is my test file.

aa, bb, cc
dd, ee
ff, gg, hh

Is it possible to make awk think we only have 2 fields for each record and the record separator is the first ,? So in my example, the $2 for each record is:

bb, cc
gg, hh
  • Probably easier to pre-process this with another awk / sed script to remove extra comma's
    – al76
    May 6, 2019 at 1:10
  • 3
    I don't think there's any. Use sub instead: awk '{sub(/^[^,]*, */,"")} {print}'
    – user313992
    May 6, 2019 at 1:57
  • 2
    awk -F'(^[^,]*, *)' '{ print $2 }' infile May 6, 2019 at 6:03
  • @αғsнιη This really works! Could you explain a little bit about the regex? I can't fully understand it. ^ always matches the start of the record, does that mean the field separator always at the beginning of the record? I don't quite understand. May 6, 2019 at 6:23
  • 1
    that sets the field seperator to a string that it starts from the beginning of a record upto first comma and including trailing spaces. [^,]*, matches every character that it's not a comma until first comma seen. May 6, 2019 at 6:42

3 Answers 3


Done by below method and it worked fine

From Above question i understood it should consider $2 and $3 as $2 only


awk -F " " '{$2=$2$3;print $1,$2}' filename

You can do that with Perl by specifying the number of fields you want your current record to be split into. In your case it is 2.

The -F option will split into two fields the current record $_ using the comma as delimiter.

$ perl -F'/,/,$_,2' -lane '
    print "\$0=<<$_>>";
    print "\$1=<<$F[0]>> \$2=<<$F[1]>>", "\n";
' file.csv

You could also use the read builtin of bash gainfully here by giving two arguments to it :

  while IFS=, read -r f1 f2; do
     # $1 = f1 $2 = f2
  done < file.csv 

Yes, you can do this if you just cram all fields after $2 into $2.

awk '
  BEGIN{ FS=","; OFS=";" }
    for(i=3; i<=NF; i++) {
      $2 = $2","$i
  { print $1,$2 }
 ' data.txt

Will give you these results:

aa; bb, cc
dd; ee
ff; gg, hh

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .