0

I need to replace 2 things from a csv file using Unix shell scripting ; one is Y with 'Y and the newline character by ',' value..

my csv:(values will be one below another vertically and all values start with Y)

YC1234
YC5678

expected output is NEEDED IN A LINE horizontally like :

'YC1234','YC5678'

kindly help as i am new to shell scripting...

i tried sed by its difficult in removing newline

cat master_upd.csv | sed -e 's//'\'','\''/g' | sed 's/\n/ /'
Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
user3116123
  • 129
  • 2
  • 7

3 Answers3

0
tr '\n' ',' < master.csv

The first command 'cat' is probably unnecessary.

thefourtheye
  • 607
  • 1
  • 6
  • 19
0

Here is a thread on replacing newlines with something else using sed: How can I replace a newline (\n) using sed?

sed -e 's/^.*$/'\''&'\''/' master_upd.csv | sed ':a;N;$!ba;s/\n/,/g'

The first sed puts in the single quotes, the second one replaces newlines with commas. Notice that I removed the cat and just told sed to open your file directly.

Community
  • 1
  • 1
Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
0
awk -v q="'" 'NR>1{printf ","}{printf "%s",q$0q}END{print ""}' file
Kent
  • 189,393
  • 32
  • 233
  • 301