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 -...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
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 |
|
|||
|
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 |
|
|||
|
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 |
|
|||
|
* 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' |
|
|||
|
* 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' |
![]() |
| Thread Tools | |
| Display Modes | |
|
|