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
ee
gg, hh
Done by below method and it worked fine
From Above question i understood it should consider $2 and $3 as $2 only
command
awk -F " " '{$2=$2$3;print $1,$2}' filename
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 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
subinstead:awk '{sub(/^[^,]*, */,"")} {print}'awk -F'(^[^,]*, *)' '{ print $2 }' infile^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.[^,]*,matches every character that it's not a comma until first comma seen.