I'm writing a bash script which calls vim to modify another file, then join all the lines in the file using '\n'. Code I tried in script:
vi filefff (then I modify the text in filefff)
cat filefff
new=$(cat filefff | sed 'N;N;s/\n/\\n/g')
echo $new
Here is the problem:
for example, if there are two lines in the file: first-line aa, second-line bb,
aa
bb
then I change the file to:
aa
bb
cc
dd
ee
the result of echo $new
is aa"\n"bb cc"\n"dd ee"\n".The command only joined some of the lines.
And then I append some more lines:
aa
bb
cc
dd
ee
ff
gg
hh
the result is aa"\n"bb cc"\n"dd ee"\n"ff, the 'hh' is gone.
So I'd like to know why and how to join all the lines with '\n', no matter how many lines I'm going to append to the file.