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
# 8  
Old 05-26-2011
Firstly, start using code tags when posting code/data samples, thanks!

Secondly, your OS doesn't have 'nawk' - try using either 'awk' or 'gawk' instead!
# 9  
Old 05-26-2011
Smilievgersh99, It worked. Thanks a lot. Let me run through real data. Good JOb.

---------- Post updated at 03:38 PM ---------- Previous update was at 03:33 PM ----------

One more questions on the output. The output has now 800 columns. Excel can load only 256 columns. Any idea how to load all the columns?
# 10  
Old 05-26-2011
See if this works:
Code:
#!/usr/bin/ksh
typeset -i mFld=0
IFS=','
mFields=""
for mLine in $(head -1 input_file)
do
  mFld=${mFld}+1
  mFound=$(echo ${mLine} | egrep 'kkk')
  if [[ "${mFound}" != "" ]]; then
    mFields=${mFields}${mFld}','
  fi
done
mFields=${mFields%,}
cut -d',' -f"${mFields}" input_file

# 11  
Old 05-26-2011
Quote:
Originally Posted by arv_cds
Smilievgersh99, It worked. Thanks a lot. Let me run through real data. Good JOb.

---------- Post updated at 03:38 PM ---------- Previous update was at 03:33 PM ----------

One more questions on the output. The output has now 800 columns. Excel can load only 256 columns. Any idea how to load all the columns?
By not using Excel? Smilie Maybe others would know - I'm not Excel savvy.
I can modify the code to create multiple files 256 (or less) columns each.
Let me know.
# 12  
Old 05-26-2011
What will this do? I have many columns with zero output. those can be deleted, if all the raw for that columns are zero.

---------- Post updated at 03:46 PM ---------- Previous update was at 03:45 PM ----------

That will help. To split file in 250 columns in each file.

---------- Post updated at 03:50 PM ---------- Previous update was at 03:46 PM ----------

Keep the first colunm of the original file in each file. Thanks

---------- Post updated at 04:06 PM ---------- Previous update was at 03:50 PM ----------

Vgersh,

To reduce the columns, Is it possible to add all the fields and make two columns? First and total off all the extracted columns.

In my original file:
kkk kkk kkk Total
1 2 5 8
3 1 1 5

That is what I am doing in excel adding up all the raws to one column.

Thanks
# 13  
Old 05-26-2011
Don't quite follow your 'First and total off all the extracted columns', but try this:
Code:
awk '
   FNR==1{
        for(i=1;i<=NF;i++)
            if ($i ~ str) {
              if (!h) h=$i
              f=(f)?f FS i:i
            }
        print h FS "TOTAL"
        nf=split(f,fA,FS);next
   }
   {
       t=0
       printf("%s", $1 FS)
       for(i=1;i<=nf;i++)
           t+=$fA[i]
       printf("%d\n", t)
   }' str='Commands' FS=, esx09.tst

# 14  
Old 05-27-2011
Vgersh,

Thanks a lot, it worked.
To clarify the data: The input file has first columns as timestamp, rest of the fields are number under different headre. Your awk should keep first field, extract columns with string "Commands" from the rest of the columns. Add (sum) of all the rows of extracted and put in a new columns called Total. The output would have two columns, first timestamp same as input file and 2nd column is Total field.

In my original file:
Code:
Time kkk kkk kkk Total
9:15AM 1 2 5 8 
9:30AM 3 1 1 5

Keep the bold field from above with "," for output.

Pl confirm that is what you did.

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:06 AM.. Reason: once again - please use code tags!
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