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
  • 109,738
  • 11
  • 156
  • 185
Manish Verma
  • 751
  • 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
  • 55,641
  • 6
  • 55
  • 99
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
  • 10,557
  • 3
  • 26
  • 45
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
  • 252,262
  • 96
  • 511
  • 570
0

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

awk '{printf $0 (/"$/?RS:X)}' file
BMW
  • 38,908
  • 11
  • 90
  • 109