Need awk help to print specific columns with as string in a header


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need awk help to print specific columns with as string in a header
# 15  
Old 05-27-2011
Code:
awk '
   FNR==1{
        h=$1
        for(i=1;i<=NF;i++)
            if ($i ~ str) {
              h=h OFS $i
              f=(f)?f FS i:i
            }
        print h OFS "TOTAL"
        nf=split(f,fA,FS);next
   }
   {
       t=0
       printf("%s", $1)
       for(i=1;i<=nf;i++) {
           printf("%c%d", OFS, $fA[i])
           t+=$fA[i]
       }
       printf("%c%d\n", OFS,t)
   }' str='Commands' OFS=, esx09.tst

# 16  
Old 05-27-2011
Sorry, I may have missed something. The final output should have only two columns. First timestamp and 2nd Total. Your last night script had good output. The new one has multiple columns.

Output example:

Code:
Timestamp,Total
   9:15AM,8
   9:30AM,7

My explaination in prev post was to confirm your logic in last night's script.

Thanks,
Arv
Moderator's Comments:
Mod Comment
Please use code tags when posting data and code samples!

Last edited by vgersh99; 05-27-2011 at 10:53 AM.. Reason: once AGAIN - please use code tags!
# 17  
Old 05-27-2011
Code:
awk '
   FNR==1{
        print $1 OFS "Total"
        for(i=1;i<=NF;i++)
            if ($i ~ str)
              f=(f)?f FS i:i
        nf=split(f,fA,FS)
        next
   }
   {
       t=0
       for(i=1;i<=nf;i++)
           t+=$fA[i]
       printf("%s%c%d\n", $1, OFS,t)
   }'  str='Commands' OFS=, esx09.tst

# 18  
Old 05-27-2011
Vgresh, The total columns has all zeros. Last night script has good output. Just confirm it totals the "Commands" field and we are done with this post.

Thanks,
Arv
# 19  
Old 05-27-2011
Most likely this's because you keep changing your original file format: yesterday it had ',' as separators - today it's separated by spaces.
Which is it?
If the original file is space separated:
Code:
str='Commands' OFS=, esx09.tst

If the original file is ',' separated:
Code:
str='Commands' FS=, OFS=, esx09.tst

# 20  
Old 05-27-2011
Vgresh,

Thanks again. It worked as last night's script. The input is "," separated file.
Regards,
-Arv
# 21  
Old 05-27-2011
Quote:
Originally Posted by arv_cds
Vgresh,

Thanks again. It worked as last night's script. The input is "," separated file.
Regards,
-Arv
So, we're good then?
This User Gave Thanks to vgersh99 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to print multiple required columns dynamically in a file using the header name?

Hi All, i am trying to print required multiple columns dynamically from a fie. But i am able to print only one column at a time. i am new to shell script, please help me on this issue. i am using below script awk -v COLT=$1 ' NR==1 { for (i=1; i<=NF; i++) { ... (2 Replies)
Discussion started by: balu1234
2 Replies

2. Shell Programming and Scripting

awk to print string if tag is specific value

In the below awk I am trying to print expName only if another tag planExecuted is true. In addition to the expName I am also printing planShortID. For some reason the word experiment gets printed so I remove it with sed. I have attached the complete index.html as well as included a sample of it... (1 Reply)
Discussion started by: cmccabe
1 Replies

3. Shell Programming and Scripting

awk - how to print specific field if a string is matched

hi gurus, I would like to be able to use awk to process 1 file as such: abc 1 2 3 4 5 6 7 8 9 10 flags 1 2 4 flags 1 2 5 abc 2 3 4 5 6 7 8 9 10 11 flags 1 2 3 abc 4 5 6 7 8 9 6 7 78 89 flags 1 2 3 flags 1 2 4 flags 1 2 3 4 I would like to be able to print field 1 and 5 when the... (4 Replies)
Discussion started by: revaroo
4 Replies

4. UNIX for Dummies Questions & Answers

How to Detect Specific Pattern and Print the Specific String after It?

I'm still beginner and maybe someone can help me. I have this input: the great warrior a, b, c and what i want to know is, with awk, how can i detect the string with 'warrior' string on it and print the a, b, and c seperately, become like this : Warrior Type a b c Im still very... (3 Replies)
Discussion started by: radynaraya
3 Replies

5. Shell Programming and Scripting

Print columns matching to specific values

Hello Friends, I have a CDR file and i need to print out 2 columns with their field position which matches to some constant values, a part of input file CZ=1|CZA=1|DIAL=415483420001|EE=13|ESF=1|ET=|FF=0|9|MNC=99|MNP=9041|MTC=0|NID=2|NOA=international|ON=1| OutPut ... (3 Replies)
Discussion started by: EAGL€
3 Replies

6. Shell Programming and Scripting

awk compare specific columns from 2 files, print new file

Hello. I have two files. FILE1 was extracted from FILE2 and modified thanks to help from this post. Now I need to replace the extracted, modified lines into the original file (FILE2) to produce the FILE3. FILE1 1466 55.27433 14.72050 -2.52E+03 3.00E-01 1.05E+04 2.57E+04 1467 55.27433... (1 Reply)
Discussion started by: jm4smtddd
1 Replies

7. Shell Programming and Scripting

awk print specific columns one row at a time

Hello, I have the following piece of code: roleName =`cat $inputFile | awk -F';' '{ print $1 }'` roleDescription =`cat $inputFile | awk -F';' '{ print $2 }'` roleAuthProfile =`cat $inputFile | awk -F';' '{ print $3 }'` mappedUserID (5 Replies)
Discussion started by: pr0tocoldan
5 Replies

8. Shell Programming and Scripting

Extract columns where header matches a given string

Hi, I'm having trouble pulling out columns where the headers match a file of key ID's I'm interested in and was looking for some help. file1.txt I Name 34 56 84 350 790 1215 1919 7606 9420 file2.txt I Name 1 1 2 2 3 3 ... 34 34... 56 56... 84 84... 350 350... M 1 A A A A... (20 Replies)
Discussion started by: flotsam
20 Replies

9. Shell Programming and Scripting

awk or sed command to print specific string between word and blank space

My source is on each line 98.194.245.255 - - "GET /disp0201.php?poc=4060&roc=1&ps=R&ooc=13&mjv=6&mov=5&rel=5&bod=155&oxi=2&omj=5&ozn=1&dav=20&cd=&daz=&drc=&mo=&sid=&lang=EN&loc=JPN HTTP/1.1" 302 - "-" "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; .NET CLR 1.0.3705; .NET CLR... (5 Replies)
Discussion started by: elamurugu
5 Replies

10. Shell Programming and Scripting

search a word and print specific string using awk

Hi, I have list of directory paths in a variable and i want to delete those dirs and if dir does not exist then search that string and get the correct path from xml file after that delete the correct directory. i tried to use grep and it prints the entire line from the search.once i get the entire... (7 Replies)
Discussion started by: dragon.1431
7 Replies
Login or Register to Ask a Question