Is there a awk solution for this??


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Is there a awk solution for this??
# 8  
Old 03-14-2008
Quote:
Originally Posted by aspect_p
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.
Of course,
line by line.

Code:
NR == 1 { print "lead1   07q4    07q1    06q4    06q3    sept    oct     nov     tot" }

Just print the header (you may prefere to use BEGIN, instead of NR == 1, but in that case you should use the -v syntax for the users variable:

Code:
awk -v users="tim|tad"

Code:
$1 ~ "^("users")$" && NF > 1

+ for every record that matches the pattern:

- first field is one of the names given (the variable users is defined at the end: users="tim|tad" , the pipe | means alternation, tim OR tad.
- AND (logical and) the number of fields is greater than 1, this is specific for the OP input file format, we want to skip the following lines:

Code:
        lead1    2008q1
        tom
        pat
        tim
        tad


+ do the following:

Code:
{ 
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
}

- x is an associative array, the keys are the names ($1), the value/element is their count (how many times they appear in the matched lines). So when the name appears for the first time: x[$1] == 1:

Code:
tim    357    03     04     25     3020   3120   20
or
tad    1734   0      0      0      5213   5213   0

Save fields 1, 2, 5, 6 and 7 as value of another associative array p with key $1 (the name), sprintf is needed for formatting (values separated by tabs).
- the t array is the sum of $2 and $5 and uses the same key.

Code:
if (x[$1] == 3)

- if x[$1] is 3, the following lines:

Code:
tim    0      21     1800   0
or
tad    0      0      672    0

Code:
{
  printf "%s\t%s\t%s\t%s\t%d\n", p[$1], $2, $3, $4, t[$1] 
 }


print the previously saved (when p[$1] was 1 ) record (p[$1]) plus $2 (sept), $3 (oct), $4 (nov) and the sum.


Login or Register to Ask a Question

Previous Thread | Next Thread

6 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk solution for Splitting a file.

Hi I have a csv file with as below sdg-catalog-00000001 sdg-sku-00000317 sdg-sku-00000318 sdg-sku-00000319 sdg-sku-00000320 sdg-catalog-00000002 sdg-sku-00000321 sdg-sku-00000322 sdg-sku-00000323 sdg-sku-00000324 sdg-sku-00000325 sdg-catalog-00000003 sdg-sku-00000326... (3 Replies)
Discussion started by: Raghuram717
3 Replies

2. UNIX for Dummies Questions & Answers

Help with awk solution to add columns

Hi all. Wondering if someone can help with an awk solution to a problem I'm stumped with. I have a matrix file with >1000 fields and would like to add another column after each column with a text label. For example: Input: $cat file.txt name col1 col2 col3 coln aaaa ... (2 Replies)
Discussion started by: torchij
2 Replies

3. Shell Programming and Scripting

sed or awk Solution

Hi I am having a csv file like this ahsh,90.82,add,32424,ahha hhdh,98.89,hdhdh,92728,neha hshs,you,97.7,hdhdhd,are,a jsjsj,wonderful,9788,79.9,aheh ahdh,95.5,girl, 2737,jolllI need to add width="100" to the value which is greater than 90 like decimal points but less than 100 Output... (5 Replies)
Discussion started by: kshitij
5 Replies

4. Shell Programming and Scripting

Any solution with awk for volatile columns??

Hi I have this file with content ale,4 ,ale,2 ,ale,1 ,ale,2 ale,1 ,ale,7 ,ale,7 ,ale,13 ale,6 ,ale,1 ,ale,1 ,ale,1 ale,1 ,ale,1 ,ale,37 ,ale,1 ale,1 ,ale,1 ,ale,2 ,ale,37 ale,77 ,ale,1 ,ale,53 ,ale,3 ale,5 ,ale,1 ,ale,2 ,ale,40 ale,1 ,ale,1 ,ale,44 ,ale,1... (7 Replies)
Discussion started by: nikhil jain
7 Replies

5. Shell Programming and Scripting

AWK or SED solution

Hello. I have big file data like this(part of file): .... 18210102021010000110 47401000000 021001 5166891.16 021011 5166891.16 18210602010020000110 47401000000 020701 8995421.00 021001 8995421.00 021011 8995421.00 030801 .08 18210604011020000110 47401000000 020701 9048.00 021001... (3 Replies)
Discussion started by: maxoff
3 Replies

6. Shell Programming and Scripting

Awk solution

Hello! Well, I searched and wasn't able to find a specific example of my dilemma, so hopefully someone could assist? Or maybe there was an example but I missed it? I have two files: file1 = order data file file2 = list of 65,000+ order numbers I would like to extract from 'file1' any... (5 Replies)
Discussion started by: rm -r *
5 Replies
Login or Register to Ask a Question