Unix Shell - converting 1 column data into 2 columns

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > September 2007 > converting 1 column data into 2 columns





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author converting 1 column data into 2 columns
tropicflite

2007-09-15, 7:16 pm

This should be an easy puzzle to solve. I have data in the following
format:

1
1
2
2
2
3
4
4
6
7
7

What I want is to convert the data so that the first column shows the
data, and the second number shows the number of occurrences (I'm going
to feed it to gnuplot).

So the converted file should look like this:

1 2
2 3
3 1
4 2
6 1
7 2

Any help would be appreciated.

TIA,

tropicflite

Janis Papanagnou

2007-09-15, 7:16 pm

tropicflite wrote:
> This should be an easy puzzle to solve. I have data in the following
> format:
>
> 1
> 1
> 2
> 2
> 2
> 3
> 4
> 4
> 6
> 7
> 7
>
> What I want is to convert the data so that the first column shows the
> data, and the second number shows the number of occurrences (I'm going
> to feed it to gnuplot).
>
> So the converted file should look like this:
>
> 1 2
> 2 3
> 3 1
> 4 2
> 6 1
> 7 2
>
> Any help would be appreciated.


uniq -c datafile | awk '{print $2,$1}'


Janis

>
> TIA,
>
> tropicflite
>

Ed Morton

2007-09-16, 1:31 am

tropicflite wrote:
> This should be an easy puzzle to solve. I have data in the following
> format:
>
> 1
> 1
> 2
> 2
> 2
> 3
> 4
> 4
> 6
> 7
> 7
>
> What I want is to convert the data so that the first column shows the
> data, and the second number shows the number of occurrences (I'm going
> to feed it to gnuplot).
>
> So the converted file should look like this:
>
> 1 2
> 2 3
> 3 1
> 4 2
> 6 1
> 7 2
>
> Any help would be appreciated.
>
> TIA,
>
> tropicflite
>


If you don't care about preserving the input order:

awk '{c[$0]++}END{for (i in c) print i,c[i]}' file

Ed.
William James

2007-09-16, 1:31 am

On Sep 15, 6:38 pm, tropicflite <tropicfl...@gmail.com> wrote:
> This should be an easy puzzle to solve. I have data in the following
> format:
>
> 1
> 1
> 2
> 2
> 2
> 3
> 4
> 4
> 6
> 7
> 7
>
> What I want is to convert the data so that the first column shows the
> data, and the second number shows the number of occurrences (I'm going
> to feed it to gnuplot).
>
> So the converted file should look like this:
>
> 1 2
> 2 3
> 3 1
> 4 2
> 6 1
> 7 2
>
> Any help would be appreciated.
>
> TIA,
>
> tropicflite


awk 'prev!="" && $1!=prev {print prev,cnt; cnt=0}
{prev=$1; cnt++}
END{print prev,cnt}' myfile

William James

2007-09-16, 1:31 am

On Sep 15, 6:38 pm, tropicflite <tropicfl...@gmail.com> wrote:
> This should be an easy puzzle to solve. I have data in the following
> format:
>
> 1
> 1
> 2
> 2
> 2
> 3
> 4
> 4
> 6
> 7
> 7
>
> What I want is to convert the data so that the first column shows the
> data, and the second number shows the number of occurrences (I'm going
> to feed it to gnuplot).
>
> So the converted file should look like this:
>
> 1 2
> 2 3
> 3 1
> 4 2
> 6 1
> 7 2
>
> Any help would be appreciated.
>
> TIA,
>
> tropicflite


ruby -e 'a=[[gets]]; ARGF.each{|s|
a << [] if a[-1][0] != s
a[-1] << s }
a.each{|x| puts [x.last.chop,x.size].join(" ") }' file

tropicflite

2007-09-16, 1:31 am

Thank you all for your answers.

Janis, yours is a great solution! Since my data is not in order, I
simply added a sort command like so:

sort unsortedfile | uniq -c | awk '{print $2,$1}' >gnuplotdata

Ed, gnuplot wants the data pre-sorted, so your solution does have that
limitation.

William, both of your solutions work perfectly. I didn't have ruby
installed, but apt-get ruby took care of that in a jif.

In case any of you have a need to generate bar graphs from large
unsorted data files, try the above one-liner, and then plot it in
gnuplot with this short script:

set title "Bar Graph" font "Monospace,18"
set grid
set xlabel "All of the numbers from the data list"
set ylabel "Number of occurrences of each number"
set nokey
set xrange [0:8]
set xtics 2
set mxtics 2
set yrange [0:4]
set ytics 1
set boxwidth 0.5
set style fill solid 1 border -1
plot 'gnuplotdata' with boxes lt 1

I named this script bgg (bar graph generator), and I run it by
starting gnuplot from the directory where the gnuplotdata file is and
typing

load "bgg"

at the gnuplot prompt.

I hope this helps someone, and I thank you all for helping me.

tropicflite

William James

2007-09-16, 7:26 am

On Sep 15, 9:34 pm, tropicflite <tropicfl...@gmail.com> wrote:
> Thank you all for your answers.
>
> Janis, yours is a great solution! Since my data is not in order, I
> simply added a sort command like so:
>
> sort unsortedfile | uniq -c | awk '{print $2,$1}' >gnuplotdata
>
> Ed, gnuplot wants the data pre-sorted, so your solution does have that
> limitation.
>
> William, both of your solutions work perfectly. I didn't have ruby
> installed, but apt-get ruby took care of that in a jif.


Please note that both of my solutions operate on
contiguous runs of numbers. E.g.,

35
35
8
8
8
35

becomes

35 2
8 3
35 1

So I guess you'll want to sort first.

>
> In case any of you have a need to generate bar graphs from large
> unsorted data files, try the above one-liner, and then plot it in
> gnuplot with this short script:
>
> set title "Bar Graph" font "Monospace,18"
> set grid
> set xlabel "All of the numbers from the data list"
> set ylabel "Number of occurrences of each number"
> set nokey
> set xrange [0:8]
> set xtics 2
> set mxtics 2
> set yrange [0:4]
> set ytics 1
> set boxwidth 0.5
> set style fill solid 1 border -1
> plot 'gnuplotdata' with boxes lt 1
>
> I named this script bgg (bar graph generator), and I run it by
> starting gnuplot from the directory where the gnuplotdata file is and
> typing
>
> load "bgg"
>
> at the gnuplot prompt.
>
> I hope this helps someone, and I thank you all for helping me.
>
> tropicflite



Jorge Moratilla Porras

2007-09-16, 1:24 pm

On 16 sep, 01:38, tropicflite <tropicfl...@gmail.com> wrote:
> This should be an easy puzzle to solve. I have data in the following
> format:
>
> 1
> 1
> 2
> 2
> 2
> 3
> 4
> 4
> 6
> 7
> 7
>
> What I want is to convert the data so that the first column shows the
> data, and the second number shows the number of occurrences (I'm going
> to feed it to gnuplot).
>
> So the converted file should look like this:
>
> 1 2
> 2 3
> 3 1
> 4 2
> 6 1
> 7 2
>
> Any help would be appreciated.
>
> TIA,
>
> tropicflite


Try this little script in perl
#!/usr/bin/perl
while (<> ) {
chomp;
$hash{$_}++;
}

foreach $i (sort keys %hash) {
print "$i $hash{$i}\n";
}

call it script.pl and execute it as:
perl script.pl < file.txt

Regards
jorge moratilla

Steven Mocking

2007-09-16, 1:24 pm

Jorge Moratilla Porras schreef:
> On 16 sep, 01:38, tropicflite <tropicfl...@gmail.com> wrote:
*snip*[vbcol=seagreen]
>
> Try this little script in perl


Uniq, awk, ruby, perl... Of course TIMTOWTDI, but how will this end?
PROLOG, C, Assembly language?
Allodoxaphobia

2007-09-17, 1:29 pm

On Sun, 16 Sep 2007 17:39:04 +0200, Steven Mocking wrote:
> Jorge Moratilla Porras schreef:
> *snip*
>
> Uniq, awk, ruby, perl... Of course TIMTOWTDI, but how will this end?
> PROLOG, C, Assembly language?


Anyone want a version in FOCAL?
William James

2007-09-17, 7:22 pm

On Sep 17, 12:52 pm, Allodoxaphobia <bit-buc...@config.com> wrote:
> On Sun, 16 Sep 2007 17:39:04 +0200, Steven Mocking wrote:
>
>
>
> Anyone want a version in FOCAL?


I'd like to see a version in J or Nial (QNial, Q'Nial).

Cyrus Kriticos

2007-09-18, 1:27 pm

Steven Mocking wrote:
> Jorge Moratilla Porras schreef:
> *snip*
>
> Uniq, awk, ruby, perl... Of course TIMTOWTDI, but how will this end?
> PROLOG, C, Assembly language?


What's the name of this newsgroup? comp.unix.SHELL ;)

[bash]

while read x; do let a[$x]=a[$x]+1; done < input.dat
for x in ${!a[@]}; do echo $x ${a[$x]}; done

--
Best regards | "The only way to really learn scripting is to write
Cyrus | scripts." -- Advanced Bash-Scripting Guide
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com