awk question - print columns with names


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers awk question - print columns with names
# 1  
Old 08-05-2014
awk question - print columns with names

I found this command and would like to know what it means:

Code:
gawk 'NR==1{for(i=1;i<=NF;i++)if($i~/FE/)f[n++]=i}{for(i=0;i<n;i++)printf"%s%s",i?" ":"",$f[i];print""}'

It seems to mean if the row =1 assign i to that row, and then if FE is in the top row /column then increment and print the row.

I am at a loss as to what i<n means. And also the f[n++]. Anybody familiar with awk? The command seems to print a column based on the name of the column instead of the number of the column
# 2  
Old 08-05-2014
Code:
gawk 'NR==1{ # For the very first line ( NR is line number )
        for(i=1;i<=NF;i++) # Loop over all columns from 1 to NF (number of fields)
                if($i~/FE/)f[n++]=i # If the field contains NE, set F[n]=(field number).  n increments itself because of the ++.
                # I wonder if that line is supposed to be f[n++]=$i, to save the column itself.
        }
        { # For every line
                for(i=0;i<n;i++) # Loop from 0 to n, from the block above
                        printf "%s%s",i?" ":"",$f[i] # Print each column in array f in order.
                        # For the first column, print nothing before it.
                        # For every other column, print : first.
                        print "" # Print a blank line, just to put a newline at the end
        }'

This User Gave Thanks to Corona688 For This Post:
# 3  
Old 08-05-2014
Quote:
Originally Posted by Corona688
Code:
gawk 'NR==1{ # For the very first line ( NR is line number )
        for(i=1;i<=NF;i++) # Loop over all columns from 1 to NF (number of fields)
                if($i~/FE/)f[n++]=i # If the field contains NE, set F[n]=(field number).  n increments itself because of the ++.
                # I wonder if that line is supposed to be f[n++]=$i, to save the column itself.
        }
        { # For every line
                for(i=0;i<n;i++) # Loop from 0 to n, from the block above
                        printf "%s%s",i?" ":"",$f[i] # Print each column in array f in order.
                        # For the first column, print nothing before it.
                        # For every other column, print : first.
                        print "" # Print a blank line, just to put a newline at the end
        }'

Close. Comments below are related to the text marked in red above:
  1. "NE" should be "FE".
  2. No. F[n++]=i is correct. F[0] will be the field number of the 1st field with a header containing "FE"; F[1] will be the field number of the 2nd field with a header containing "FE", ... F[n-1] will be the field number of the nth field containing "FE". Since there is no next command associated with the NR==1 condition, the header for all fields containing "FE" will be printed by the next clause.
  3. The character printed before the 1st field output is an empty string; the output field separator output before other fields is a single <space> character.
This User Gave Thanks to Don Cragun 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 use "awk" to print columns from different files in separate columns?

Hi, I'm trying to copy and paste the sixth column from a bunch of files into a single file having each column pasted in separate columns (and not one after each other in just one column.) I tried this code but works only partially because it copied and pasted 50 rows of each column... (6 Replies)
Discussion started by: Frastra
6 Replies

2. Shell Programming and Scripting

awk print columns and variable

Hi, Can anyone help with the below please? I have written some code which takes an input file, and and prints the contents out to a new file - it then loops round and prints the same columns, but increments the ID column by 1 each time. Input file; NAME,1,15-Dec-15, NAME,1,21-Dec-15,... (9 Replies)
Discussion started by: Ads89
9 Replies

3. Shell Programming and Scripting

awk print columns which are not null

I am working on a file with several columns as below MO_NAME,FAULT_TYPE,CLASS,CODE1,CODE2,CODE3 RXOCF-101,BTS INTERNAL,FAULT CODES CLASS 2A,53,58 RXOCF-101,BTS INTERNAL,FAULT CODES CLASS 2B,24 RXOCF-101,BTS INTERNAL,FAULT CODES CLASS 2A,33 RXOCF-101,BTS INTERNAL,FAULT CODES CLASS 2D,57 ... (12 Replies)
Discussion started by: Rizwan Rasul
12 Replies

4. Shell Programming and Scripting

Awk: is it possible to print into multiple columns?

Hi guys, I have hundreds file like this, here I only show two of them: file 1 feco4_s_BB95.log ZE_1=-1717.5206260 feco4_t_BB95.log ZE_1=-1717.5169250 feco5_s_BB95.log ZE_1=-1830.9322060... (11 Replies)
Discussion started by: liuzhencc
11 Replies

5. Shell Programming and Scripting

Print unique names in a specific column using awk

Is it possible to modify file like this. 1. Remove all the duplicate names in a define column i.e 4th col 2. Count the no.of unique names separated by ";" and print as a 5th col thanx in advance!! Q input c1 30 3 Eh2 c10 96 3 Frp c41 396 3 Ua5;Lop;Kol;Kol c62 2 30 Fmp;Fmp;Fmp ... (5 Replies)
Discussion started by: quincyjones
5 Replies

6. Shell Programming and Scripting

Print unique names in each row of a specific column using awk

Is it possible to remove redundant names in the 4th column? input cqWE 100 200 singapore;singapore AZO 300 400 brazil;america;germany;ireland;germany .... .... output cqWE 100 200 singapore AZO 300 400 brazil;america;germany;ireland (4 Replies)
Discussion started by: quincyjones
4 Replies

7. Shell Programming and Scripting

awk: print columns depending on their number

hi guys, i have the following problem: i have a matrix with 3 columns and over 450 rows like this: 0.0165 0.0151 0.0230 0.0143 0.0153 0.0134 0.0135 0.0123 0.0195 0.0173 0.0153 0.0182 i now want to calculate the average of every line and divide every element of this... (1 Reply)
Discussion started by: waddle
1 Replies

8. Shell Programming and Scripting

Removing columns from awk '{ print $0 }'

I have a one-line command, lsusb | awk '{ $1=""; $2=""; $3=""; $4=""; $5=""; $6=""; print $0 }' It works, and gives the results I expect, I was just wondering if I am missing some easier way to nullify the first 6 column variables? Something like, lsusb | awk '{ $(1-6)=""; print $0 }' But... (10 Replies)
Discussion started by: AlphaLexman
10 Replies

9. Shell Programming and Scripting

using awk to print some columns of a file

Hi, i have a file with content 00:01:20.613 'integer32' 459254 00:01:34.158 459556 00:01:36.626 'integer32' 459255 and i want to print only output as below 00:01:20.613 459254 00:01:34.158 459556 00:01:36.626 459255 i dont want the word 'integer32' which is the second column. i... (2 Replies)
Discussion started by: dealerso
2 Replies

10. Shell Programming and Scripting

cannot print the columns i want with awk.

hi friends! i have a script where a execute a veritas command, available_media wich retrieves me a list of tapes .lst then i execute cat /tmp/listtapes.lst | grep -v VL |sed '/^$/d'|awk -F, '{print $1, $3, $4, $9} ' > /tmp/media1.lst but it prints all the columns instead of the four... (3 Replies)
Discussion started by: pabloli150
3 Replies
Login or Register to Ask a Question