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 have a file riddled with this kind of thing:

-POEM-
>> The big brown fox
>> jumps over the
>> lazy dog.
The end.

I want to get rid of "\n>>" and just turn this into a oneliner:

-POEM-
The big brown fox jumps over the lazy dog.
The end.

I'm trying the following, all of which refuse to work:

perl -pe 's/\r?\n>>//g' task.tex
tr '\n\>\>' '' < task.tex 
sed -i ':a;N;$!ba;s/\n>>//g' task.tex 

What am I doing wrong?

share|improve this question

3 Answers 3

This worked for me:

while read line; do
         line1=`echo $line`;
         if [ `echo $line1 | grep "^>" | wc -l` -eq 1 ];
         then
                 var1=`echo $line1 | sed 's/^...//'`;
                 line2=`echo $line2 $var1`;
         else
                 echo $line2;
                 echo $line1;
         fi;
done < task.tex
share|improve this answer
    
It is not necessary to fork a new subshell process just to assign the contents of a shell variable to another shell variable. –  JdeBP Feb 21 at 9:13
    
Thanks for aligning the code.. please also let me know a generic code for doing the above mentioned task!!! As the answer by Ketan is input specific. –  Vipul Feb 21 at 11:11

Assuming the text is in a file called x.txt:

tr -d '\n>>' < x.txt | sed -e 's/-POEM-/&\n/' -e 's/\./&\n/g'
share|improve this answer

Why do you have to run through so much of trouble? Here's what I just did with your piece of text.

  1. Open the file in GEdit.

  2. Keep the cursor on the start of the second line

  3. Use CTRL + H (Find and replace)

  4. Find for >> and replace with SPACE

  5. Find for \n and replace with SPACE

Make sure you use REPLACE ALL in both cases (4 and 5).

That's what I did and it worked :)

share|improve this answer
    
This would work just fine. Not sure why you would need to over complicate such a task. –  Matthew Williams Feb 21 at 9:12

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.