Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question
add comment

3 Answers

up vote 7 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
share|improve this answer
add comment

A very very simple way to do it.

cat inputfile.txt | while read a
do
echo $a >> outputfile.txt
echo "" >> outputfile.txt
done
share|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
add comment

sed can do this quite easily

sed 'G' file
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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