问题
i have a file which contains several instances of \n.
i would like to replace them with actual newlines, but sed doesn't recognize the \n.
i tried
sed -r -e 's/\n/\n/'
sed -r -e 's/\\n/\n/'
sed -r -e 's/[\n]/\n/'
and many other ways of escaping it.
is sed able to recognize a literal \n? if so, how?
is there another program that can read the file interpreting the \n's as real newlines?
回答1:
Can you please try this
sed -i 's/\\n/\n/g' input_filename
回答2:
$ echo "\n" | sed -e 's/[\\][n]/hello/'
回答3:
awk
seems to handle this fine:
echo "test \n more data" | awk '{sub(/\\n/,"**")}1'
test ** more data
Here you need to escape the \
using \\
回答4:
1) sed work 1 line a t a time son no \n on 1 line only (it's removed by sed at read time into buffer). You should use N,n or H, h to fill the buffer with more than 1 line, and than \n appear inside. Be carreful, ^ and $ are no more end of line but end of string/buffer so \n are inside. 2) \n is recognize in search pattern, not in replace pattern. 2 ways for using it (sample)
sed s/\(\n\)bla/\1blabla\1/
sed s/\nbla/\
blabla\
/
first use a \n already inside as back reference (smaller line in replace pattern) second use a real new line
so basicaly
sed "N
$ s/\(\n\)/\1/g
"
work (but is a bit useless). I imagine that s/\(\n\)\n/\1/g
is more what you want
来源:https://stackoverflow.com/questions/19762365/sed-help-matching-and-replacing-a-literal-n-not-the-newline