0

I am working on a CSV file like this

"EF", "12345", "Test, String", "Xyz" and I need output as

"EF", "12345", "Test$$$ String", "Xyz"

basically I need to replace the comma with $$$

3
  • Iam sorting a huge CSV file (~4GB) based on certain columns. The CSV file is comma separated.I intend to use unix sort which is faster, however I have some rows which contain comma(,)in the row values.How do I sort in such case as the number of columns will increase if (,) is used as delimiter. Example: Here I want to sort based on 2 and and 4th column, but if I use (,) as delimiter i will get total 5 columns in 1st row and 4 columns in second row that would spoil the sorting. "wert","DGC","Xyt, temp","3456" "wert","ABC","ppp","1234" Commented Aug 26, 2015 at 14:56
  • eliminate a whole class of problems from your data processing and start exporting your data with the | character as delimiter. Good luck. Commented Aug 26, 2015 at 15:02
  • Thank you for the answer, but replacing the delimiter is something which is not in my control, so Iam looking for alternatives Commented Aug 26, 2015 at 15:06

4 Answers 4

3

You can use gnu-awk for this:

awk 'BEGIN{OFS=", "; FPAT="\"[^\"]+\""} {sub(/,/, "$$$", $3)} 1' file
"EF", "12345", "Test$$$ String", "Xyz"
Sign up to request clarification or add additional context in comments.

Comments

2

You can try something like this:

, ([^"])

And replace by:

$$$ $1

Regex live here.


Or even this:

([^"]),

And replace by:

$1$$$

Regex live here.

4 Comments

my sed and awk don't support lookahead/behind. yours does?
Thank you, but the regex fails for "EF", "12345", ",Test String", "Xyz" Can this case also be handled
@jsphdnl. And what is the new expected result? Can you update the question?
Failed if "field", ", ", ", ", "field" occur
0
sed ':a
     s/^\(\([[:blank:]]*"[^"]*",\)*[[:blank:]]*"[^",]*\),/\1\$\$\$/
     t a
     ' YourFile
  • posix version
  • use recursive remove of ; inside string delimited by " (so not the fastest)
  • assuming there is no escaped " inside string (if the case, need to adapt a bit)

Comments

0

It works with sub as well.
awk '{sub(/Test,/,"Test$$$")}1' file
"EF", "12345", "Test$$$ String", "Xyz"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.