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
# 1  
Old 05-26-2011
Need awk help to print specific columns with as string in a header

awk experts,

I have a big file of 4000 columns with header. Would like to print the columns with string value of "Commands" in header. File has "," separator. This file is on ESX host with Bash.

Thanks,
Arv
# 2  
Old 05-26-2011
Could you please display a sample input (a few columns is enough) and desired output?
# 3  
Old 05-26-2011
Sure.
Input with 4000 columns.
abc,xxkkk,eeee,eee,kkknn,sss,xxx,kkkvvv,yyy, ttt,mmm,fff..........
1,1,3,4,6,7,8,9,9,0,0,5
3,4,1,5,1,5,6,7,8,9,9,7,

Would like to print the colums with header which has substring of KKK
xxkkk, kkknn,kkkvvv
1,6,9
4,1,7

Thanks,
# 4  
Old 05-26-2011
See if this works for you:
Code:
cut -d',' -f2,5,8 input_file

# 5  
Old 05-26-2011
Thanks. This works for the test file but not for the real file which has 4000 columns. and the substring "kkk" could be in any column and may be in 1000 different columns. Need an if statement in awk which can scan each header and find the header with string 'KKK' and then enter in print to select the column. Ask questions or put in your words to make sure that I am explaining it well.

Thanks, Again.
# 6  
Old 05-26-2011
depending on your version of awk, there might be a limit on the maximum number of fields per record. If so, try using gawk.

Code:
nawk '
   FNR==1{
        for(i=1;i<=NF;i++) 
            if ($i ~ str) {
              h=(h)?h FS $i:$i
              f=(f)?f FS i:i
            }
        print h
        nf=split(f,fA,FS);next
   }
   {
       for(i=1;i<=nf;i++) 
           printf("%s%c",$fA[i], (i==nf)?ORS:FS)
   }' str='kkk' FS=, myFile

# 7  
Old 05-26-2011
I got following error: What did I missed?

Code:
[root@esx09 ~]# ./esx09.scr
./esx09.scr: line 14: nawk: command not found
[root@esx09 ~]# more esx09.scr
nawk '
   FNR==1{
        for(i=1;i<=NF;i++)
            if ($i ~ str) {
              h=(h)?h FS $i:$i
              f=(f)?f FS i:i
            }
        print h
        nf=split(f,fA,FS);next
   }
   {
       for(i=1;i<=nf;i++)
           printf("%s%c",$fA[i], (i==nf)?ORS:FS)
   }' str='Commands' FS=, esx09.tst
[root@esx09 ~]# ./esx09.scr
./esx09.scr: line 14: nawk: command not found

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

Last edited by vgersh99; 05-26-2011 at 05:24 PM.. Reason: code tags, please!
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