unixnotepadtrbbedit

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


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'

Solution

  • 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','