Combining fields in awk

is it easy 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 two numbers as the first two fields, but then I want all the text (commas and everything) to be the third field. Is there a way to do this?

My main awk call was as follows:

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

      

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

+1


source to share


2 answers


May be:



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

      

+2


source


You can use sed for this.



cat file.txt | sed 's/\(.?*\),\(.?*\),/\1|\2|/'

      

0


source







All Articles