Replace newline character with ',' in Notepad or BBEdit or using Unix command `tr`?
Asked Answered
H

5

5

I have to replace the newline character with ',' for using in some oracle command but in a single command. I can't figure out how to do it.

Input:

R1
R2
R3
R4

Required Output:

'R1','R2','R3','R4'
Haematogenous answered 24/12, 2012 at 5:25 Comment(0)
C
2

Using tr:

cat data.txt | tr '\n' ','

If you need the quotes, you could pipe to sed:

cat data.txt | tr '\n' ',' | sed "s/,/','/g"

… which gets you pretty close:

R1','R2','R3','R4','
Charla answered 24/12, 2012 at 5:33 Comment(1)
I am new to unix so could not understand why we can not directly replace newline with ',' charactersHaematogenous
C
4

In BBedit, you would use a grep like these for find:

(R[0-9]+)\r

and replace:

'\1', 
Catarinacatarrh answered 24/12, 2012 at 14:17 Comment(0)
C
2

Using tr:

cat data.txt | tr '\n' ','

If you need the quotes, you could pipe to sed:

cat data.txt | tr '\n' ',' | sed "s/,/','/g"

… which gets you pretty close:

R1','R2','R3','R4','
Charla answered 24/12, 2012 at 5:33 Comment(1)
I am new to unix so could not understand why we can not directly replace newline with ',' charactersHaematogenous
D
2

Using sed:

 sed -n '1h;1!H;${ g; s/\n/,/g; s/\([^,]*\)/'\''\1'\''/gp}' input
Demi answered 24/12, 2012 at 5:44 Comment(8)
Thanks for replying ... while firing the command on terminal I am getting an error:" sed: 1: "1h;1!H;${ g; s/\n/,/g; ...": bad flag in substitute command: '}'"Haematogenous
it works even when I disable gnu extensions. Are you missing something during copy-paste?Demi
I have pasted the following " sed -n '1h;1!H;${ g; s/\n/,/g; s/([^,]*)/'\''\1'\''/gp}' <aitr" where aitr is my file nameHaematogenous
@Jagdeep: You are missing the escapes before the parentheses. ([^,]*) needs to be \([^,]*\)Valdes
I am pasting correctly on the terminal using escapes properly but while pasting here in the comment it is escaping the value...Haematogenous
This looks wrong: s/([^,]*)/ it needs to be s/\([^,]*\)/.Demi
@Jagdeep, what is the output of echo $SHELL on your terminal?Demi
@perreal: o/p is /bin/bashHaematogenous
V
2

Here's one way using sed:

sed ":a;N;\$!ba;s/\n/','/g;s/^\|$/'/g" file

Results:

'R1','R2','R3','R4'
Valdes answered 24/12, 2012 at 6:0 Comment(1)
Thanks for replying while firing the command I am getting an error; "-bash: !ba: event not found"Haematogenous
R
1

The requirement to replace newlines does not match your sample output. To replace newlines with ,, you can do tr \\n ,, but this gives you output with a trailing comma that does not have a trailing newline. Also, it does not quote your input. Perhaps you are looking for:

paste -s -d, input

Or, if you do actually want the fields to be quoted:

< input sed "s/^\|$/'/g" | paste -s -d,

In the above, your input file is named input. The final command can also be written:

sed "s/^\|$/'/g" input | paste -s -d,
Recreation answered 24/12, 2012 at 13:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.