Re align in one row using sed

Hi,

Anyone can help on how to re align my data in one row using sed.

test.csv

"url1","abc","project url1"
2016-08-16
"url2,"microsoft","project url2"
2016-08-18

need output like this

"url1","abc","project url1","2016-08-16"
"url2,"microsoft","project url2","2016-08-18"

Thanks in advance

Regards,
FSPalero

In awk

[akshay@localhost tmp]$ cat f
"url1","abc","project url1"
2016-08-16
"url2,"microsoft","project url2"
2016-08-18
[akshay@localhost tmp]$ awk '{printf("%s%s%s",NF==1?OFS:"",$0,NF==1?RS:"")}' OFS=, f
"url1","abc","project url1",2016-08-16
"url2,"microsoft","project url2",2016-08-18

---------- Post updated at 09:58 AM ---------- Previous update was at 09:56 AM ----------

If you need quote then

[akshay@localhost tmp]$ awk 'NF==1{$0=q $0 q}{printf("%s%s%s",NF==1?OFS:"",$0,NF==1?RS:"")}' q='"' OFS=, f
"url1","abc","project url1","2016-08-16"
"url2,"microsoft","project url2","2016-08-18"

Hi Akshay Hegde,

Thank you very much for the help, it work!

Regards,
Feride

Not sure this will work, and I can't test right now:

sed 's/$/,"/; N; s/$/"/' test.csv

And another one:

$ sed '/^"/ {N; s/\n/,/}' test.csv
"url1","abc","project url1",2016-08-16
"url2,"microsoft","project url2",2016-08-18

Hi.

Assuming you can use standard commands and do not need quotes on date:

paste -d, - - < test.csv

producing

"url1","abc","project url1",2016-08-16
"url2,"microsoft","project url2",2016-08-18

Best wishes ... cheers, drl

1 Like

A bit simplified, extra quotes in red:

awk '{printf "%s",(NF==1)?(",\"" $0 "\"" RS):$0}' test.csv

A Unix/GNU sed solution:

sed '$!N; s/\n\([^,]*\)$/,"\1"/' test.csv

Corrected (and tested) version:

sed 's/$/,"/; N; s/$/"/; s/\n//' test.csv
"url1","abc","project url1","2016-08-16"
"url2,"microsoft","project url2","2016-08-18"

and that is simplified

sed 'N; s/$/"/; s/\n/,"/' test.csv

and has the further advantage that GNU sed will print a last odd line as is.