reverse effect of uniq -c

This is a discussion on reverse effect of uniq -c within the Linux General forums, part of the Linux Forums category; Hi, I am wondering if there is a simple shell manipulation that can produce the reverse effect of using uniq -...


Go Back   Usenet Forums > Linux Forums > Linux General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-07-2008
A
 
Posts: n/a
Default reverse effect of uniq -c

Hi,

I am wondering if there is a simple shell manipulation that can
produce the reverse effect of using uniq -c. In other words, how can I
create a file x.txt so that when I type 'uniq -c x.txt' on the shell,
I get the following output?


4 "0" "1" "1" "1" "1"
31 "0" "2" "0" "0" "0"
142 "0" "2" "0" "0" "1"
173 "0" "2" "0" "0" "2"
1 "0" "2" "0" "0" "NA"
1 "0" "2" "0" "NA" "1"
1 "0" "NA" "NA" "NA" "1"
1 "1" "0" "2" "2" "0"
18 "1" "1" "1" "1" "0"
41 "1" "1" "1" "1" "1"
1 "1" "1" "1" "NA" "0"
3 "1" "1" "1" "NA" "1"
112 "1" "2" "0" "0" "0"
340 "1" "2" "0" "0" "1"
4 "1" "2" "0" "0" "2"
2 "1" "2" "0" "NA" "1"
9 "1" "NA" "1" "1" "0"
12 "1" "NA" "1" "1" "1"
1 "1" "NA" "1" "1" "NA"
1 "1" "NA" "1" "NA" "1"
1 "1" "NA" "2" "NA" "1"
2 "2" "0" "2" "2" "0"
27 "2" "1" "1" "1" "0"
94 "2" "2" "0" "0" "0"
1 "2" "2" "0" "0" "1"
1 "2" "2" "0" "0" "NA"
6 "2" "NA" "1" "1" "0"
1 "2" "NA" "1" "NA" "0"
1 "NA" "2" "0" "0" "1"


Thanks,
A
Reply With Quote
  #2 (permalink)  
Old 04-07-2008
Chris F.A. Johnson
 
Posts: n/a
Default Re: reverse effect of uniq -c

On 2008-04-07, A wrote:
> Hi,
>
> I am wondering if there is a simple shell manipulation that can
> produce the reverse effect of using uniq -c. In other words, how can I
> create a file x.txt so that when I type 'uniq -c x.txt' on the shell,
> I get the following output?


Feed the output of uniq-c to:

awk '{ n = $1; sub( ".*" $1 " " ,"") ; while ( n-- ) print }'

> 4 "0" "1" "1" "1" "1"
> 31 "0" "2" "0" "0" "0"
> 142 "0" "2" "0" "0" "1"

....
> 1 "2" "2" "0" "0" "NA"
> 6 "2" "NA" "1" "1" "0"
> 1 "2" "NA" "1" "NA" "0"
> 1 "NA" "2" "0" "0" "1"



--
Chris F.A. Johnson, author | <http://cfaj.freeshell.org>
Shell Scripting Recipes: | My code in this post, if any,
A Problem-Solution Approach | is released under the
2005, Apress | GNU General Public Licence
Reply With Quote
  #3 (permalink)  
Old 04-08-2008
A
 
Posts: n/a
Default Re: reverse effect of uniq -c

Here's is a sample output of uniq -c

bash-3.1$ cat temp.txt

4 0 1 1 1 1 0 1 2 2 2
6 0 2 0 0 0 0 0 2 2 1
6 0 2 0 0 0 0 1 1 2 1
9 0 2 0 0 0 0 1 2 2 1
3 0 2 0 0 0 0 2 0 2 2
3 0 2 0 0 0 0 2 1 2 2
3 0 2 0 0 0 0 2 2 2 2
1 0 2 0 0 0 9 2 1 2 2


Everything looks fine except the last line of the output:

bash-3.1$ awk '{ n = $1; sub( ".*" $1 " " ,"") ; while ( n-- )
print }' temp.txt
0 1 1 1 1 0 1 2 2 2
0 1 1 1 1 0 1 2 2 2
0 1 1 1 1 0 1 2 2 2
0 1 1 1 1 0 1 2 2 2
0 2 0 0 0 0 0 2 2 1
0 2 0 0 0 0 0 2 2 1
0 2 0 0 0 0 0 2 2 1
0 2 0 0 0 0 0 2 2 1
0 2 0 0 0 0 0 2 2 1
0 2 0 0 0 0 0 2 2 1
0 2 0 0 0 0 1 1 2 1
0 2 0 0 0 0 1 1 2 1
0 2 0 0 0 0 1 1 2 1
0 2 0 0 0 0 1 1 2 1
0 2 0 0 0 0 1 1 2 1
0 2 0 0 0 0 1 1 2 1
0 2 0 0 0 0 1 2 2 1
0 2 0 0 0 0 1 2 2 1
0 2 0 0 0 0 1 2 2 1
0 2 0 0 0 0 1 2 2 1
0 2 0 0 0 0 1 2 2 1
0 2 0 0 0 0 1 2 2 1
0 2 0 0 0 0 1 2 2 1
0 2 0 0 0 0 1 2 2 1
0 2 0 0 0 0 1 2 2 1
0 2 0 0 0 0 2 0 2 2
0 2 0 0 0 0 2 0 2 2
0 2 0 0 0 0 2 0 2 2
0 2 0 0 0 0 2 1 2 2
0 2 0 0 0 0 2 1 2 2
0 2 0 0 0 0 2 1 2 2
0 2 0 0 0 0 2 2 2 2
0 2 0 0 0 0 2 2 2 2
0 2 0 0 0 0 2 2 2 2
2 2






On Apr 7, 5:41 pm, "Chris F.A. Johnson" <cfajohn...@gmail.com> wrote:
> On 2008-04-07, A wrote:
> > Hi,

>
> > I am wondering if there is a simple shell manipulation that can
> > produce the reverse effect of using uniq -c. In other words, how can I
> > create a file x.txt so that when I type 'uniq -c x.txt' on the shell,
> > I get the following output?

>
> Feed the output of uniq-c to:
>
> awk '{ n = $1; sub( ".*" $1 " " ,"") ; while ( n-- ) print }'
>
>
>
> > 4 "0" "1" "1" "1" "1"
> > 31 "0" "2" "0" "0" "0"
> > 142 "0" "2" "0" "0" "1"

> ...
> > 1 "2" "2" "0" "0" "NA"
> > 6 "2" "NA" "1" "1" "0"
> > 1 "2" "NA" "1" "NA" "0"
> > 1 "NA" "2" "0" "0" "1"

>
> --
> Chris F.A. Johnson, author | <http://cfaj.freeshell.org>
> Shell Scripting Recipes: | My code in this post, if any,
> A Problem-Solution Approach | is released under the
> 2005, Apress | GNU General Public Licence


Reply With Quote
  #4 (permalink)  
Old 04-08-2008
pk
 
Posts: n/a
Default Re: reverse effect of uniq -c

A wrote:

> Everything looks fine except the last line of the output:


Try this:

awk '{n=$1;gsub(/^[[:digit:]]+ /,"",$0);while(n--)print}' temp.txt

Reply With Quote
  #5 (permalink)  
Old 04-08-2008
Ralf Fassel
 
Posts: n/a
Default Re: reverse effect of uniq -c

* A <born.to.b.wyld@gmail.com>
| bash-3.1$ awk '{ n = $1; sub( ".*" $1 " " ,"") ; while ( n-- ) print }' temp.txt

This sub() statement substitutes everything up to the _last_ occurence
of $1 in the line, which in the case of

1 0 2 0 0 0 9 2 1 2 2

leaves only the last two digits. Pure coincidence that the other
lines did not include the count in the lines itself.

First set $1 to empty string, then remove the space at the beginning
of the line
awk '{ n = $1; $1 = "" ; sub("^ +",""); while ( n-- ) print }' test.txt

HTH
R'
Reply With Quote
  #6 (permalink)  
Old 04-08-2008
A
 
Posts: n/a
Default Re: reverse effect of uniq -c

Thanks. Those suggestions help.
Reply With Quote
  #7 (permalink)  
Old 04-08-2008
Ralf Fassel
 
Posts: n/a
Default Re: reverse effect of uniq -c

* A <born.to.b.wyld@gmail.com>
| > awk '{ n = $1; $1 = "" ; sub("^ +",""); while ( n-- ) print }' test.txt
| Thanks. Those suggestions help.

Just for the record: note that changing $1 might have an impact on the
rest of the line if the other fields are separated by more than one
space:

echo "1 2 4 5 6" | awk '{$1 = ""; print}'
=> 2 4 5 6
and not " 2 4 5 6" as one might expect.

YMMV
R'
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 12:25 PM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0