Discussion:
count in colum
MOKRANI Rachid
2013-03-29 13:43:44 UTC
Permalink
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.

__________________________
Avant d'imprimer, pensez à l'environnement ! Please consider the environment before printing !
Ce message et toutes ses piÚces jointes sont confidentiels et établis à l'intention exclusive de ses destinataires. Toute utilisation non conforme à sa destination, toute diffusion ou toute publication, totale ou partielle, est interdite, sauf autorisation expresse. IFP Energies nouvelles décline toute responsabilité au titre de ce message. This message and any attachments are confidential and intended solely for the addressees. Any unauthorised use or dissemination is prohibited. IFP Energies nouvelles should not be liable for this message.
__________________________
Thierry Blanc
2013-03-29 14:33:54 UTC
Permalink
try something like that

cut -d ';' -f 1 your_file | uniq -c | sed -r 's|\s*([0-9]*)
([0-9]*)|\2;\1;|'

sed can't count, so use uniq to do the counting.
Post by MOKRANI Rachid
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.
__________________________
Avant d'imprimer, pensez à l'environnement ! Please consider the environment before printing !
Ce message et toutes ses piÚces jointes sont confidentiels et établis à l'intention exclusive de ses destinataires. Toute utilisation non conforme à sa destination, toute diffusion ou toute publication, totale ou partielle, est interdite, sauf autorisation expresse. IFP Energies nouvelles décline toute responsabilité au titre de ce message. This message and any attachments are confidential and intended solely for the addressees. Any unauthorised use or dissemination is prohibited. IFP Energies nouvelles should not be liable for this message.
__________________________
------------------------------------
Daniel
2013-03-30 05:08:18 UTC
Permalink
I "cleaned it up" a little, using same idea.

head -1 infile > outfile
tail -n +2 infile | cut -d ';' -f 1 | uniq -c > counts
sed -r 's|\s*([0-9]*) ([0-9]*)|\2 ; \1 ;|' counts >> outfile

I still think it would be easier to work with if you did not have the spaces around the semicolons. :)
Post by Thierry Blanc
try something like that
cut -d ';' -f 1 your_file | uniq -c | sed -r 's|\s*([0-9]*)
([0-9]*)|\2;\1;|'
sed can't count, so use uniq to do the counting.
Post by MOKRANI Rachid
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.
__________________________
Avant d'imprimer, pensez à l'environnement ! Please consider the environment before printing !
Ce message et toutes ses piÚces jointes sont confidentiels et établis à l'intention exclusive de ses destinataires. Toute utilisation non conforme à sa destination, toute diffusion ou toute publication, totale ou partielle, est interdite, sauf autorisation expresse. IFP Energies nouvelles décline toute responsabilité au titre de ce message. This message and any attachments are confidential and intended solely for the addressees. Any unauthorised use or dissemination is prohibited. IFP Energies nouvelles should not be liable for this message.
__________________________
------------------------------------
RAKESH
2013-03-30 09:24:57 UTC
Permalink
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
Post by MOKRANI Rachid
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.
Thierry Blanc
2013-03-30 10:22:42 UTC
Permalink
I confess, sed CAN count!
Post by RAKESH
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
Post by MOKRANI Rachid
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.
------------------------------------
Aurelio Jargas
2013-03-30 15:41:47 UTC
Permalink
There's always the amazing Greg Ubben's dc.sed calculator to prove that sed
is a lot more than a stream editor :)

http://sed.sourceforge.net/local/scripts/dc.sed.html
Post by Thierry Blanc
I confess, sed CAN count!
Post by RAKESH
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
Post by MOKRANI Rachid
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
--
Aurelio | www.aurelio.net | @oreio


[Non-text portions of this message have been removed]
Werfgam Nadler
2013-04-16 09:35:56 UTC
Permalink
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 <***@hotmail.com>
To: sed-***@yahoogroups.com
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
Post by MOKRANI Rachid
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]
Loading...