Question

I am using:

cat this_file | sed ':a;N;$!ba;s/\n[a-z]/ [a-z]/g' > that_file

Sample text:

However the text \n
looks like this when it replaces the newline.\n

Output:

However the text [a-z]ooks like this when it replaces the newline.\n

What is this called? Any suggestions on what I am doing wrong?

Was it helpful?

Solution

Within the pattern, [a-z] denotes a character class. Within the replacement, it is substituted as is. You need to use a backreference:

cat this_file | sed ':a;N;$!ba;s/\n\([a-z]\)/ \1/g' > that_file

Moreover, you could avoid useless use of cat:

sed ':a;N;$!ba;s/\n\([a-z]\)/ \1/g' this_file > that_file
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top