converting 1 column data into 2 columns

converting 1 column data into 2 columns

am 16.09.2007 01:38:52 von Matt Nichols

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

Re: converting 1 column data into 2 columns

am 16.09.2007 02:03:00 von Janis Papanagnou

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
>

Re: converting 1 column data into 2 columns

am 16.09.2007 02:30:44 von Ed Morton

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.

Re: converting 1 column data into 2 columns

am 16.09.2007 02:35:55 von William James

On Sep 15, 6:38 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.
>
> TIA,
>
> tropicflite

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

Re: converting 1 column data into 2 columns

am 16.09.2007 03:02:48 von William James

On Sep 15, 6:38 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.
>
> 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

Re: converting 1 column data into 2 columns

am 16.09.2007 04:34:49 von Matt Nichols

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

Re: converting 1 column data into 2 columns

am 16.09.2007 08:41:36 von William James

On Sep 15, 9:34 pm, tropicflite 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

Re: converting 1 column data into 2 columns

am 16.09.2007 14:52:57 von Jorge Moratilla Porras

On 16 sep, 01:38, 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

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

Re: converting 1 column data into 2 columns

am 16.09.2007 17:39:04 von Steven Mocking

Jorge Moratilla Porras schreef:
> On 16 sep, 01:38, tropicflite wrote:
>> This should be an easy puzzle to solve. I have data in the following
>> format:
*snip*
>> Any help would be appreciated.
>>
>> TIA,
>>
>> tropicflite
>
> Try this little script in perl

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

Re: converting 1 column data into 2 columns

am 17.09.2007 19:52:43 von Allodoxaphobia

On Sun, 16 Sep 2007 17:39:04 +0200, Steven Mocking wrote:
> Jorge Moratilla Porras schreef:
>> On 16 sep, 01:38, tropicflite wrote:
>>> This should be an easy puzzle to solve. I have data in the following
>>> format:
> *snip*
>>> Any help would be appreciated.
>>>
>> Try this little script in perl
>
> Uniq, awk, ruby, perl... Of course TIMTOWTDI, but how will this end?
> PROLOG, C, Assembly language?

Anyone want a version in FOCAL?

Re: converting 1 column data into 2 columns

am 17.09.2007 20:53:32 von William James

On Sep 17, 12:52 pm, Allodoxaphobia wrote:
> On Sun, 16 Sep 2007 17:39:04 +0200, Steven Mocking wrote:
> > Jorge Moratilla Porras schreef:
> >> On 16 sep, 01:38, tropicflite wrote:
> >>> This should be an easy puzzle to solve. I have data in the following
> >>> format:
> > *snip*
> >>> Any help would be appreciated.
>
> >> Try this little script in perl
>
> > Uniq, awk, ruby, perl... Of course TIMTOWTDI, but how will this end?
> > PROLOG, C, Assembly language?
>
> Anyone want a version in FOCAL?

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

Re: converting 1 column data into 2 columns

am 18.09.2007 19:55:09 von Cyrus Kriticos

Steven Mocking wrote:
> Jorge Moratilla Porras schreef:
>> On 16 sep, 01:38, tropicflite wrote:
>>> This should be an easy puzzle to solve. I have data in the following
>>> format:
> *snip*
>>> Any help would be appreciated.
>>>
>>> TIA,
>>>
>>> tropicflite
>>
>> Try this little script in perl
>
> 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