I need a bash script that will take a file and add a second \n character for every \n character already in the file:

that is:

abcd\nbcda

becomes

abcd\n\nbcda

how do I start. OR, should I write it in another language and wrap it in bash?

link|improve this question

feedback

3 Answers

up vote 6 down vote accepted
sed 'G' file > newfile

or

perl -nae 'print "$_\n";' file > newfile

or

while read ln
do
   echo $ln; echo;
done < file > newfile
link|improve this answer
feedback

A very very simple way to do it.

cat inputfile.txt | while read a
do
echo $a >> outputfile.txt
echo "" >> outputfile.txt
done
link|improve this answer
wouldn't that be echo '\n' >>outputfile.txt or does echo automatically put in a newline? – mechko Feb 1 '10 at 17:14
1  
@mechko - Yes, echo automatically puts in a newline – Nifle Feb 1 '10 at 17:18
1  
use echo -n if you don't want echo to automatically output a newline. – quack quixote Feb 1 '10 at 19:06
feedback

sed can do this quite easily

sed 'G' file
link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.