1

I have data in a file in this format as given below

"253539","","Company1 Name  
","0","1"
"255229","","Ramu ","0","1"
"253548","","Mater Name/Id 
","0","1"
"255229","","Ram Lakh","0","1"
"253619","","CHild Name/Ids 
","0","1"

Every line has \n appended to it.

How can I replace \n", to ", in shell script to get the output as

"253539","","Company1 Name/Id  ","0","1"
"255229","","Ramu ","0","1"
"253548","","Mater Name/Id ","0","1"
"255229","","Ram Lakh","0","1"
"253619","","CHild Name/Id ","0","1"

Need to automate a process so have to write in shell script.

Please help.

that other guy
  • 116,971
  • 11
  • 170
  • 194
Manish Verma
  • 771
  • 7
  • 20

4 Answers4

1

Not sure about doing this easily in shell, but I made a throw-away Perl script to do it:

perl -e 'foreach (<>) { chop; print; print "\n" if /"$/ }' < mydata.csv

It works by outputting a newline only if the line ends in ".

paddy
  • 60,864
  • 6
  • 61
  • 103
0

Could do something like

#!/bin/bash

while read -r line; do
 [[ $line =~ '"'$ ]] && echo "$line" || echo -n "$line"
done < file

e.g. on your input file

> ./abovescript
"253539","","Company1 Name","0","1"
"255229","","Ramu ","0","1"
"253548","","Mater Name/Id","0","1"
"255229","","Ram Lakh","0","1"
"253619","","CHild Name/Ids","0","1"
Reinstate Monica Please
  • 11,123
  • 3
  • 27
  • 48
0

With awk you can do:

$ awk '/"$/ {print a$0; a=""; next} {a=$0}' file
"253539","","Company1 Name  ","0","1"
"255229","","Ramu ","0","1"
"253548","","Mater Name/Id ","0","1"
"255229","","Ram Lakh","0","1"
"253619","","CHild Name/Ids ","0","1"

What it does is to store in a the line in case it does not end with ". If it does, it prints the stored line plus the current one.

fedorqui
  • 275,237
  • 103
  • 548
  • 598
0

if @fedorqui's answer can be accepted, then here is the shorter code.

awk '{printf $0 (/"$/?RS:X)}' file
BMW
  • 42,880
  • 12
  • 99
  • 116