Re: count in colum
Werfgam Nadler <[email protected]>
| Newsgroups | gmane.editors.sed.user |
|---|---|
| Message-ID | <[email protected]> |
Hi,
A small issue with ur solution.
if the user input is like
12 ; 1 ;
12 ; 2 ;
12 ; 1 ;
then expected solution is
12 ; 4 ;
not with urs. i.e if the count is more than '1' ur solution bounces.
solution with count more than 1
##########################BEGIN########################
1n;
N;
s/ //g;
/^\([^;]*\);\(.*\)\n\1;/!{
s/;/ ; /g;
P;
D;
}
s/^\(.*\);\(.*\);\n\(.*\);\(.*\);/&\n(\2)(\4)(0123456789)/;
:loop
{
/(00*)/bloop2;
:sub
{
/_/!s/.)/_&/2;
/_\([^0]\)\([^)]*\))(\([^)]*\)\(.\)\1/{
s//_\4\2)(\3\4\1/;
s/_//;
badd;
}
/[0-9]_0/{
s/\(.\)_0/_\19/;
bsub;
}
}
:add
{
/_/!s/.)/_&/1;
/_\([^9]\)\([^)]*\))(\([^)]*\))(\([^)]*\)\1\(.\)/{
s//_\5\2)(\3)(\4\1\5/;
s/_//;
bloop;
}
/[0-9]_9/{
s/\(.\)_9/_\10/;
badd;
}
s/(_9/(10/;
}
bloop;
}
:loop2
{
s/_//g;
s/^\(.*\);\(.*\);\n\(.*\);\(.*\);\n(\([^)]*\))(\([^)]*\)).*$/\n\1 ; \5 ; /
D;
}
##########################END########################
thanks
W
________________________________
From: RAKESH <[email protected]>
To: [email protected]
Sent: Saturday, March 30, 2013 2:54 PM
Subject: Re: count in colum
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.
------------------------------------
--
Yahoo! Groups Links
[Non-text portions of this message have been removed]