0

So I have the following script working as a daily workaround until the dev people fix:

#!/bin/sh

sed -i 's/,/;/g' /file_path/filename_date.csv
sed -i 's/NAME;NAME/NAME,NAME/g' /file_path/filename_date.csv

It works but when I look at it I think something is missing or isn't elegant. Maybe is my way of thinking being so much straight forward.

What is your opinion?

anmoreira
  • 3
  • 2
  • What is the script supposed to do? Replace all `,` (except those between `NAME` and `NAME`) with a semicolon? – chepner Jun 30 '20 at 13:47
  • Yes. when doing the first one it replaces all ```,``` with ```;``` but at one field ```NAME,NAME``` doing that command changes the ```,``` with ```;``` and I need that field to be with ```,``` if not, importing the csv file gets messed up – anmoreira Jun 30 '20 at 14:41

2 Answers2

0

Like this:

sed -i 's/,/;/g; s/NAME;NAME/NAME,NAME/g' file

or

sed -i -e 's/,/;/g' -e 's/NAME;NAME/NAME,NAME/g' file
Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223
  • 1
    Thank you for replies. I was complicating too much on my head but it is still straight forward your suggestions. – anmoreira Jun 30 '20 at 14:43
0

Maybe with extended regular expressions and capture groups:

sed -Ei 'y/,/;/;s/(NAME);\1/\1,\1/g'

The y command is for transliteration.

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116