Let's say I have a CSV file:
"col1","col2","col3"
"col4","col5,subtext","col6
The problem I have is as follows :
cut -d, -f1,2 test.txt
"coll1","col2"
"col4","col5
The desired output is :
"col1","col2"
"col4","col5,subtext"
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.
Sign up to join this communityLet's say I have a CSV file:
"col1","col2","col3"
"col4","col5,subtext","col6
The problem I have is as follows :
cut -d, -f1,2 test.txt
"coll1","col2"
"col4","col5
The desired output is :
"col1","col2"
"col4","col5,subtext"
The ParseWords module, which ships with Perl, covers this quite elegantly. Example below.
$ perl -MText::ParseWords -nE '@a=quotewords ",",1,$_;say $a[0],",",$a[1]' <test.txt
"col1","col2"
"col4","col5,subtext"
$
If you have gawk
v4 available, a fine solution exists at Parse a csv using awk and ignoring commas inside a field
Example:
gawk -vFPAT='[^,]*|"[^"]*"' '{print $1 "," $2}' test.txt
Another perl
solution, assuming all fields are quoted
$ perl -F'/"\K,(?=")/' -lane 'print "$F[0],$F[1]"' test.txt
"col1","col2"
"col4","col5,subtext"
-F'/"\K,(?=")/'
field separator is comma only if it is preceded by "
and followed by "
print "$F[0],$F[1]"
print first two fields separated by ,
Can be done with grep
as well
$ grep -oE '^"[^"]*","[^"]*"' test.txt
"col1","col2"
"col4","col5,subtext"
If N number of fields are needed, use grep -oE '^("[^"]*",){1}"[^"]*"'
where the number inside {}
is N-1
you can also try this with awk as below;
awk -F'","' '{printf "%s\",\"%s\"\n", $1, $2 }' test.txt
Eg;
user@host$ awk -F'","' '{printf "%s\",\"%s\"\n", $1, $2 }' test.txt
"col1","col2"
"col4","col5,subtext"