Question

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.

Was it helpful?

Solution

Maybe:

awk '{for (i=0;i<2;i++) sub(",", "|", $0); print}' file.txt

OTHER TIPS

You could use sed to do it.

cat file.txt | sed 's/\(.?*\),\(.?*\),/\1|\2|/'
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top