Re: count in colum
Thierry Blanc <[email protected]>
| Newsgroups | gmane.editors.sed.user |
|---|---|
| Message-ID | <[email protected]> |
I confess, sed CAN count!
On 03/30/2013 10:24 AM, RAKESH wrote:
>
> sed -e '
>
> ;# initialize the counter
> 1{x;s/.*/1/;x;}
>
> ;#just display the non-interesting line
> /^[1-9][0-9]*[ ];[ ][1][ ];$/!b
>
> ;#special processing for the last interesting line
> ${
> G
> s/^\([1-9][0-9]*[ ];\)[ ][1][ ];\n\(.*\)$/\1 \2 ;/
> b
> }
>
> ;# pick up the next line (which may or may not be an interesting line)
> N
>
> ;# two consecutive lines are the same, hence ==> counter++
> /^\(.*\)\n\1$/{
> x
> bincrement
> :bak
> x
> ;# saw off the first portion
> D
> }
>
> ;# two consecutive lines differ, hence print the first one
> ;# taking into account its counter info stored in hold space
> ;# finally also initializing the counter in preparation for
> ;# the second line (which btw is also interesting)
> G
> s/^\([1-9][0-9]*[ ];\)[ ][1][ ];\(\n.*\)\n\(.*\)$/\1 \3 ;\2/
> P
> x
> s/.*/1/
> x
> D
>
> #; consider the bottom portion as a "sed subroutine"
> #; which does ++ of a numeric number stored in the pattern space.
> :increment
> # replace all leading 9s by _
> :d
> s/9\(_*\)$/_\1/
> td
>
> # incr last digit only. The first line adds a most-significant
> # digit of 1 if we have to add a digit.
> #
> # The tn commands are not necessary, but make the thing faster
>
> s/^\(_*\)$/1\1/; tn
> s/8\(_*\)$/9\1/; tn
> s/7\(_*\)$/8\1/; tn
> s/6\(_*\)$/7\1/; tn
> s/5\(_*\)$/6\1/; tn
> s/4\(_*\)$/5\1/; tn
> s/3\(_*\)$/4\1/; tn
> s/2\(_*\)$/3\1/; tn
> s/1\(_*\)$/2\1/; tn
> s/0\(_*\)$/1\1/; tn
>
> :n
> y/_/0/
> s/^00*//
> bbak
> ' yourfile
>
>
> Have fun,
> Gudermez
>
>
> --- In [email protected], "MOKRANI Rachid" <rachid.mokrani@...> wrote:
>> Hi,
>>
>> Someone can give me a simple sed commande for doing the following.
>>
>> My file .
>>
>> Num ; Count ;
>> 8 ; 1 ;
>> 9 ; 1 ;
>> 9 ; 1 ;
>> 9 ; 1 ;
>> 10 ; 1 ;
>> 10 ; 1 ;
>> 10 ; 1 ;
>> 11 ; 1 ;
>> 12 ; 1 ;
>> 12 ; 1 ;
>>
>> I would like the following result with sed.
>>
>> Num ; Count ;
>> 8 ; 1 ;
>> 9 ; 3 ;
>> 10 ; 3 ;
>> 11 ; 1 ;
>> 12 ; 2 ;
>>
>> Any help will be really appreciate.
>> Best regards.
>
>
>
> ------------------------------------
>