tags:

views:

276

answers:

2

Hi,

is there an easy away to combine fields in awk? Specifically, I have lines like this:

1,2,some text
3,4,some text, but it has commas in it, annoyingly

I want to extract the two numbers as the first two fields, but then I want all of the text (commas and all) to be the third field. Is there a way to do that?

My basic awk call was like this:

awk -F\, '{print $1"|"$2"|"$3}' file.txt

Which works fine for the first line, but stops at the comma in the text on the second line.

Thanks!

Ben

A: 

You could use sed to do it.

cat file.txt | sed 's/\(.?*\),\(.?*\),/\1|\2|/'
PolyThinker
UUOC! (Google for it.) =)
PEZ
+2  A: 

Maybe:

awk '{for (i=0;i<2;i++) sub(",", "|", $0); print}' file.txt
PEZ
Perfect: thank you!
Ben
De nada. It's not exactly what you asked for, but it does the job. =)
PEZ