The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Is There a Sed Solution for This? racbern Shell Programming and Scripting 1 03-13-2008 11:31 AM
need solution for scripting kkc Shell Programming and Scripting 14 01-31-2008 09:17 PM
Sun and backup solution Jshwon SUN Solaris 1 10-23-2007 12:54 PM
Grep solution Amruta Pitkar Shell Programming and Scripting 3 05-03-2007 02:57 AM
Linux as a NAS solution? kjbaumann Linux 1 08-30-2006 12:09 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 03-06-2008
timj123 timj123 is offline
Registered User
  
 

Join Date: Jan 2008
Posts: 80
Is there a awk solution for this??

I am writing a awk script that gathers certain data from certain fields. I needed a awk solution for this, because it will later become a function in the script.

I have the following data that I need output on a single line, but record spans across multilple lines and records are not "together". Example would be tom below, record "tom" below is on 4 different lines, but I only need data from 2 of the lines, I will also need the same info for pat, tim, and tad, or whoever else has a record like the format below.

Code:
2008   fl01   LAC   2589   polk   doal
xx 2008q1 mx
     sect 25698541

     Sales 08 Dept group

        lead1    2008q1
        tom
        pat
        tim
        tad

        lead1  07q4   07q3   07q2   07q1   06q4   06q3   jan
        tom    0      96     0      3312   3624   0      312
        pat    0      17     0      0      30     0      30
        tim    357    03     04     25     3020   3120   20
        tad    1734   0      0      0      5213   5213   0

        lead1  feb    mar    apr    may    jun    jul    aug
        tom    0      96     0      0      0      0      0
        pat    0      17     0      0      0      0      0
        tim    357    23     5      7      8      14     70
        tad    1734   0      0      0      0      0      0

        lead1  sept   oct    nov    dec
        tom    0      0      460    92
        pat    0      0      240    0
        tim    0      21     1800   0
        tad    0      0      672    0

2008   fl01  LAC   2589    polk   doal
yy 2008q1 mx
     sect 2569852

     Sales 08 Dept group
I needed the following output:

Code:
lead1   07q4    07q1    06q4    06q3    sept    oct     nov
tim	357	25	3020	3120	0	21	1800 
tad	1734	0	5213	5213	0	0	672
Is there a awk solution to this??

thanks in advance for this, because I think this is a difficult one.
  #2 (permalink)  
Old 03-11-2008
timj123 timj123 is offline
Registered User
  
 

Join Date: Jan 2008
Posts: 80
Quote:
Originally Posted by timj123 View Post
I am writing a awk script that gathers certain data from certain fields. I needed a awk solution for this, because it will later become a function in the script.

I have the following data that I need output on a single line, but record spans across multilple lines and records are not "together". Example would be tom below, record "tom" below is on 4 different lines, but I only need data from 2 of the lines, I will also need the same info for pat, tim, and tad, or whoever else has a record like the format below.

Code:
2008   fl01   LAC   2589   polk   doal
xx 2008q1 mx
     sect 25698541

     Sales 08 Dept group

        lead1    2008q1
        tom
        pat
        tim
        tad

        lead1  07q4   07q3   07q2   07q1   06q4   06q3   jan
        tom    0      96     0      3312   3624   0      312
        pat    0      17     0      0      30     0      30
        tim    357    03     04     25     3020   3120   20
        tad    1734   0      0      0      5213   5213   0

        lead1  feb    mar    apr    may    jun    jul    aug
        tom    0      96     0      0      0      0      0
        pat    0      17     0      0      0      0      0
        tim    357    23     5      7      8      14     70
        tad    1734   0      0      0      0      0      0

        lead1  sept   oct    nov    dec
        tom    0      0      460    92
        pat    0      0      240    0
        tim    0      21     1800   0
        tad    0      0      672    0

2008   fl01  LAC   2589    polk   doal
yy 2008q1 mx
     sect 2569852

     Sales 08 Dept group
I needed the following output:

Code:
lead1   07q4    07q1    06q4    06q3    sept    oct     nov
tim	357	25	3020	3120	0	21	1800 
tad	1734	0	5213	5213	0	0	672
Is there a awk solution to this??

thanks in advance for this, because I think this is a difficult one.
any help out there for this, please?
  #3 (permalink)  
Old 03-12-2008
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,847
Code:
awk 'NR == 1 { print "lead1   07q4    07q1    06q4    06q3    sept    oct     nov" }
$1 ~ "^("users")$" && NF > 1 { 
x[$1]++
if (x[$1] == 1)
  p[$1] = sprintf ("%s\t%s\t%s\t%s\t%s", $1, $2, $5, $6, $7)
if (x[$1] == 3) {
  printf "%s\t%s\t%s\t%s\n", p[$1], $2, $3, $4 
 }
}' users="tim|tad" file
You can add more users in the pattern: tim|tad|pat etc.
Use nawk or /usr/xpg4/bin/awk on Solaris.
  #4 (permalink)  
Old 03-13-2008
timj123 timj123 is offline
Registered User
  
 

Join Date: Jan 2008
Posts: 80
Quote:
Originally Posted by radoulov View Post
Code:
awk 'NR == 1 { print "lead1   07q4    07q1    06q4    06q3    sept    oct     nov" }
$1 ~ "^("users")$" && NF > 1 { 
x[$1]++
if (x[$1] == 1)
  p[$1] = sprintf ("%s\t%s\t%s\t%s\t%s", $1, $2, $5, $6, $7)
if (x[$1] == 3) {
  printf "%s\t%s\t%s\t%s\n", p[$1], $2, $3, $4 
 }
}' users="tim|tad" file
You can add more users in the pattern: tim|tad|pat etc.
Use nawk or /usr/xpg4/bin/awk on Solaris.
This works GREAT, I REALLY appreciate the help on this, but what if I wanted to sum up, columns 07q4 and 07q1, and then put that value at the end of the printf statement? Having issues with that part. Can you help?
  #5 (permalink)  
Old 03-13-2008
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,847
Code:
awk 'NR == 1 { print "lead1   07q4    07q1    06q4    06q3    sept    oct     nov     tot" }
$1 ~ "^("users")$" && NF > 1 { 
x[$1]++
if (x[$1] == 1) {
  p[$1] = sprintf ("%s\t%s\t%s\t%s\t%s", $1, $2, $5, $6, $7)
  t[$1] = $2 + $5
}
if (x[$1] == 3) {
  printf "%s\t%s\t%s\t%s\t%d\n", p[$1], $2, $3, $4, t[$1] 
 }
}' users="tim|tad" file
  #6 (permalink)  
Old 03-13-2008
timj123 timj123 is offline
Registered User
  
 

Join Date: Jan 2008
Posts: 80
OK, I feel stupid now.
Thanks again soo much on saving me about a weeks worth of frustration.
I realize I need to look at issues like these from a different angle.
  #7 (permalink)  
Old 03-13-2008
aspect_p aspect_p is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 13
Quote:
Originally Posted by radoulov View Post
Code:
awk 'NR == 1 { print "lead1   07q4    07q1    06q4    06q3    sept    oct     nov     tot" }
$1 ~ "^("users")$" && NF > 1 { 
x[$1]++
if (x[$1] == 1) {
  p[$1] = sprintf ("%s\t%s\t%s\t%s\t%s", $1, $2, $5, $6, $7)
  t[$1] = $2 + $5
}
if (x[$1] == 3) {
  printf "%s\t%s\t%s\t%s\t%d\n", p[$1], $2, $3, $4, t[$1] 
 }
}' users="tim|tad" file
Can you go a little further in describing the awk methods used on the script, im sorry to be a bother but something like this can be a HUGE ace in my arsenal of shell scripting.
Sponsored Links
Closed Thread

Bookmarks

Tags
solaris

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 03:28 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0