Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replying to emails: how to condense multiple "blank" (not really blank; lines consisting only of ">") lines into one?

Tags:

unix

sed

I'm trying to do something like this but for quoted emails, so this

On 2014-07-11 at 03:36 PM, <[email protected]> wrote:                                                                                                                                                                                                                                                       
>Hi Everyone,                                                                                                                                                                                                                                                                                                                 
>                                                                                                                                                                                                                                                                                                                             
>                                                                                                                                                                                                                                                                                                                              
>                                                    
>I love spaces.
>                                                                                                                                                                                                                                                                                                                             
>                                                                                                                                                                                                                                                                                                                          
>                                                                                                                                                                                                                                                                                                                          
>That's all.                                                                                                                                                                                                                                                                                                                       

Would become this

On 2014-07-11 at 03:36 PM, <[email protected]> wrote:                                                                                                                                                                                                                                                       
>Hi Everyone,                                                                                                                                                                                                                                                                                                                 
>                                                                                                                                                                                                                                                                                                                             
>I love spaces.
>                                                                                                                                                                                                                                                                     
>That's all.   

Thanks

like image 657
user3843237 Avatar asked Jul 16 '14 02:07

user3843237


2 Answers

Another awk-based solution:

awk '{ /^>\s*$/?b++:b=0; if (b<=1) print }' file

Breakdown:

/^>\s*$/?b++:b=0
    - ? :       the ternary operator
    - /^>\s*$/  matches a blank line starts with ">"
    - b         variable that counts consecutive blank lines (b++).
                however, if the current line is non-blank, b is reset to 0.


if (b<=1) print
    print if the current line is non-blank (b==0)
          or if there is only one blank line (b==1).
like image 61
Shaoyun Avatar answered Oct 27 '22 02:10

Shaoyun


Assuming that each visual line is a proper logical line (string of characters ended with a \n), you can dispense with the rest of the tools and simply run uniq(1) on the input.

Example follows.

% cat tst
>Hi Everyone,
>
>
>
>I love spaces.
>
>
>
>That's all.

% uniq tst
>Hi Everyone,
>
>I love spaces.
>
>That's all.
%
like image 32
Noufal Ibrahim Avatar answered Oct 27 '22 02:10

Noufal Ibrahim