Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

The two "sed lines" only differ by a "\n" below..
How can i get this output [no new line before the </pre>]? [ http://pastebin.com/raw.php?i=ED34jvtF ]:
.
.
something3
</pre>
something4
.
.


$ echo -e 'something1\nsomething2\nsomething3\n\n</pre>\nsomething4\nsomething5'; echo "######################################"; echo -e 'something1\nsomething2\nsomething3\n\n</pre>\nsomething4\nsomething5' | sed -e N -e N -e "s#\n</pre>#</pre>#g" -e P -e D
something1
something2
something3

</pre>
something4
something5
######################################
something1
something2
something3</pre>
something4
something5
$
$ echo -e 'something1\nsomething2\nsomething3\n\n</pre>\nsomething4\nsomething5' | sed -e N -e N -e "s#\n</pre>#\n</pre>#g" -e P -e D
something1
something2
something3

</pre>
something4
something5
$

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

Give this a try:

sed 's|\(.\+\)\(</pre>\)$|\1\n\2|;t;N;\|\n</pre>$|s|^[[:blank:]]*\n||;P;D' inputfile

The result:

something1
something2
something3
</pre>
something4
something5
######################################
something1
something2
something3
</pre>
something4
something5

Notice that both instances of </pre> are on a line by themselves without a preceding blank line.

Edit: Fixed a problem with deleting the wrong lines.

share|improve this answer
    
hey, it worked!!:) no new line before the pre pastebin.com/raw.php?i=ywc16UMk THANK YOU!! god bless the open source community:) –  LanceBaynes Jan 19 '11 at 22:34
    
oops: pastebin.com/raw.php?i=0fV15Yg2 it deletes not just the empty lines before </pre> - because it deletes one line before it. i only need to delete the empty line before </pre>! but thank you! –  LanceBaynes Jan 19 '11 at 23:01
    
@user: Sorry about that, try the revised version. –  Dennis Williamson Jan 20 '11 at 0:37
    
that worked perfectly :O long live you :) thanks:) –  LanceBaynes Jan 20 '11 at 13:49
    
@user: If you found this answer useful, please mark it as accepted (you can also upvote it if you like). –  Dennis Williamson Jan 20 '11 at 15:04
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.