[CoLoCo] bash question
David Overcash
funnylookinhat at gmail.com
Fri Aug 19 21:05:25 UTC 2011
I know this isn't bash... but it's a bit easier to do and keep track of with
PHP. This assumes your columns are separated with commas...
You can just save that as "whatever.php" and then run it with "php
whatever.php" - you may need to install php... sudo apt-get install php5
Hope that helps!
<?
$file_array = file('/home/test/file1.txt');
$col2count = array();
$col3sum = 0;
foreach( $file_array as $file_line ) {
$file_line_array = explode(',',$file_line);
$col3sum += intval(trim($file_line_array[2]));
if( isset($col2count[trim($file_line[1])]) ) {
$col2count[trim($file_line[1])]++;
} else {
$col2count[trim($file_line[1])] = 1;
}
}
// print the lines
foreach( $col2count as $line => $count ) {
echo $line.' ::: '.$count."\n";
}
?>
On Fri, Aug 19, 2011 at 2:45 PM, Jim Hutchinson <jim at ubuntu-rocks.org>wrote:
> Wondering if any of you script gurus can help with a small problem. I have
> several text files containing 3 columns. I was to count the number of
> occurrences of the text in column 2 (or just count the lines) and sum column
> 3 which is a number. I know how to do the latter with something like
>
> #!/bin/bash
>
> file="/home/test/file1.txt"
> cat ${file} | \
> while read name article count
> do
> sum=$(($sum + $count ))
> echo "$sum"
> done
>
> Although that prints each sum as it goes rather than just the final sum.
>
> I'm not sure how to count text (basically counting the lines that contain
> the numbers would work the same). Also, because each file has a header row
> it's giving errors so I need to tell it to skip row 1.
>
> Finally, I want to automate the input of each file so having it read the
> list of text files from somewhere, process the file, output to a new file
> amending each time, and then repeat with the next one until all files are
> done.
>
> Any ideas?
>
> Thanks.
> --
> Jim (Ubuntu geek extraordinaire)
> ----
> Please avoid sending me Word or PowerPoint attachments.
> See http://www.gnu.org/philosophy/no-word-attachments.html
>
> --
> Ubuntu-us-co mailing list
> Ubuntu-us-co at lists.ubuntu.com
> Modify settings or unsubscribe at:
> https://lists.ubuntu.com/mailman/listinfo/ubuntu-us-co
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.ubuntu.com/archives/ubuntu-us-co/attachments/20110819/69fbbdeb/attachment.html>
More information about the Ubuntu-us-co
mailing list