Unix Shell - Add a field between lines

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > November 2007 > Add a field between lines





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 Add a field between lines
CGill

2007-11-26, 7:21 pm

I have output similar to the following:

==============
0.0 1.3 4325 infodba /opt/
0.0 1.3 4949 infodba /opt/
0.0 1.2 1671 infodba /opt/
0.0 1.5 19471 infodba /opt/
1.2 1.5 23268 infodba /opt/
0.0 2.6 73 infodba /opt/
0.0 1.4 3752 infodba /opt/
0.0 1.5 12378 infodba /opt/
==============
0.0 1.3 4325 infodba /opt/
0.0 1.3 4949 infodba /opt/
0.0 1.2 1671 infodba /opt/
0.0 1.5 19471 infodba /opt/
1.2 1.5 23268 infodba /opt/
0.0 2.6 73 infodba /opt/
0.0 1.4 3752 infodba /opt/
0.0 1.5 12378 infodba /opt/
===============

Would like to sum the first field in each line between the "==" lines
and output to a seperate file each on a new line... similar to:

================
6
================
4


Any help? I think awk is the way to go, but am by no means expert
enough in awk to figure it out...

TIA
Ed Morton

2007-11-26, 7:21 pm



On 11/26/2007 3:08 PM, CGill wrote:
> I have output similar to the following:
>
> ==============
> 0.0 1.3 4325 infodba /opt/
> 0.0 1.3 4949 infodba /opt/
> 0.0 1.2 1671 infodba /opt/
> 0.0 1.5 19471 infodba /opt/
> 1.2 1.5 23268 infodba /opt/
> 0.0 2.6 73 infodba /opt/
> 0.0 1.4 3752 infodba /opt/
> 0.0 1.5 12378 infodba /opt/
> ==============
> 0.0 1.3 4325 infodba /opt/
> 0.0 1.3 4949 infodba /opt/
> 0.0 1.2 1671 infodba /opt/
> 0.0 1.5 19471 infodba /opt/
> 1.2 1.5 23268 infodba /opt/
> 0.0 2.6 73 infodba /opt/
> 0.0 1.4 3752 infodba /opt/
> 0.0 1.5 12378 infodba /opt/
> ===============
>
> Would like to sum the first field in each line between the "==" lines
> and output to a seperate file each on a new line... similar to:


"similar to"?

> ================
> 6
> ================
> 4
>
>
> Any help? I think awk is the way to go, but am by no means expert
> enough in awk to figure it out...
>
> TIA


This should get you close:

awk '
/^=/ { if (c) print tot > "file" c; c++; tot=0; next }
{ tot += $1 }
' file

Regards,

Ed.

CGill

2007-11-26, 7:21 pm

On Nov 26, 3:31 pm, Ed Morton <mor...@lsupcaemnt.com> wrote:
> On 11/26/2007 3:08 PM, CGill wrote:
>
>
>
>
>
>
>
>
> "similar to"?
>
>
>
>
> This should get you close:
>
> awk '
> /^=/ { if (c) print tot > "file" c; c++; tot=0; next }
> { tot += $1 }
> ' file
>
> Regards,
>
> Ed.- Hide quoted text -
>
> - Show quoted text -


works well... just needed to switch the > to >> inside the brackets as
i don't want a bajillion files

Thanks!
CGill

2007-11-26, 7:21 pm

On Nov 26, 3:31 pm, Ed Morton <mor...@lsupcaemnt.com> wrote:
> On 11/26/2007 3:08 PM, CGill wrote:
>
>
>
>
>
>
>
>
> "similar to"?
>
>
>
>
> This should get you close:
>
> awk '
> /^=/ { if (c) print tot > "file" c; c++; tot=0; next }
> { tot += $1 }
> ' file
>
> Regards,
>
> Ed.- Hide quoted text -
>
> - Show quoted text -


apparently it wasn't that easy... as i'm still getting a million
files... ideas?
Ed Morton

2007-11-26, 7:21 pm



On 11/26/2007 3:50 PM, CGill wrote:
> On Nov 26, 3:31 pm, Ed Morton <mor...@lsupcaemnt.com> wrote:
>
>
> works well... just needed to switch the > to >> inside the brackets as
> i don't want a bajillion files


">" and ">>" don't mean the same thing in awk as they do in shell. In any case,
what I posted will only create one file per block as the print only gets hit on
a line starting with "=".

> apparently it wasn't that easy... as i'm still getting a million
> files... ideas?


Oh, hang on. By this:
[vbcol=seagreen]

I thought you were saying that you wanted each sum to be output to a separate
file. If you want them all in one file, change it to this:

awk '
/^=/ { if (tot != "") print $0 ORS tot; tot=0; next }
{ tot += $1 }
' infile > outfile

Ed.

CGill

2007-11-27, 1:46 am

On Nov 26, 3:59 pm, Ed Morton <mor...@lsupcaemnt.com> wrote:
> On 11/26/2007 3:50 PM, CGill wrote:
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> ">" and ">>" don't mean the same thing in awk as they do in shell. In any case,
> what I posted will only create one file per block as the print only gets hit on
> a line starting with "=".
>
>
> Oh, hang on. By this:
>
>
> I thought you were saying that you wanted each sum to be output to a separate
> file. If you want them all in one file, change it to this:
>
> awk '
> /^=/ { if (tot != "") print $0 ORS tot; tot=0; next }
> { tot += $1 }
> ' infile > outfile
>
> Ed.- Hide quoted text -
>
> - Show quoted text -


Much obliged....
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com