I unsuccesfully tried:
sed 's#/\n# #g' file
sed 's#^$# #g' file
How to fix it?
I unsuccesfully tried:
sed 's#/\n# #g' file
sed 's#^$# #g' file
How to fix it?
The perl version works the way you expected.
perl -i -p -e 's/\n//' file
Edit: As pointed out in the comments, it's worth noting that this edits in place. -i.bak
will give you a backup of the original file before the replacement in case your regex isn't as smart as you thought.
I'm not an expert, but I guess in sed
you'd first need to append the next line into the pattern space, bij using "N
". From the section "Multiline Pattern Space" in "Advanced sed Commands" of the book sed & awk (Dale Dougherty and Arnold Robbins; O'Reilly 1997; page 107 in the preview):
The multiline Next (N) command creates a multiline pattern space by reading a new line of input and appending it to the contents of the pattern space. The original contents of pattern space and the new input line are separated by a newline. The embedded newline character can be matched in patterns by the escape sequence "\n". In a multiline pattern space, the metacharacter "^" matches the very first character of the pattern space, and not the character(s) following any embedded newline(s). Similarly, "$" matches only the final newline in the pattern space, and not any embedded newline(s). After the Next command is executed, control is then passed to subsequent commands in the script.
From man sed
:
[2addr]N
Append the next line of input to the pattern space, using an embedded newline character to separate the appended material from the original contents. Note that the current line number changes.
I've used this to search (multiple) badly formatted log files, in which the search string may be found on an "orphaned" next line.
Or use this solution with sed:
sed ':a;N;$!ba;s/\n/ /g'
This will read the whole file in a loop, then replaces the newline(s) with a space.
Update: explanation.
:a
N
$!ba
(`$! means not to do it on the last line (as there should be one final newline)).a
register = the whole file.@OP, if you want to replace newlines in a file, you can just use dos2unix (or unix2dox)
dos2unix yourfile yourfile
The answer with the :a register ...
http://stackoverflow.com/questions/1251999/sed-how-can-i-replace-a-newline-n/1252191#1252191
... does not work in freebsd 7.2 on the command line:
( echo foo ; echo bar ) | sed ':a;N;$!ba;s/\n/ /g' sed: 1: ":a;N;$!ba;s/\n/ /g": unused label 'a;N;$!ba;s/\n/ /g' foo bar
But does if you put the sed script in a file or use -e to "build" the sed script...
> (echo foo; echo bar) | sed -e :a -e N -e '$!ba' -e 's/\n/ /g' foo bar
or ...
> cat > x.sed << eof
:a
N
$!ba
s/\n/ /g
eof
> (echo foo; echo bar) | sed -f x.sed
foo bar
Maybe the sed in OS X is similar.
Hi,
How can I use perl like the above example to replace two lines in a row that only contain a carriage return, followed by a string, such as "123456abcdefg"?
There are two blank lines at the top of the file, followed by the string I've listed above. I want to delete the first blank line of the file, and replace the second with a new string, terminated by the third. Unless there's a specific way to only operate on the first two lines of a file.
IOW, I'm trying to do:
perl -pi.orig -e 's/^\n\n123456abcdefg$/NewString\n123456abcefg/mg' filename
but, alas, it doesn't work. What am I missing? Am I making it too complicated? :-)
Thanks, Alex
On Mac OS X (using FreeBSD sed):
# replace each newline with a space
printf "a\nb\nc\nd\ne\nf" | sed -E -e :a -e '$!N; s/\n/ /g; ta'
printf "a\nb\nc\nd\ne\nf" | sed -E -e :a -e '$!N; s/\n/ /g' -e ta
who needs sed. bash:
cat test.txt | while read line; do echo -n "$line "; done