How to get the column number in awk?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to get the column number in awk?
# 1  
Old 11-26-2013
How to get the column number in awk?

Hi Guys,

I have a question on how i can get the column number in a file and used it in awk.

i have a file which it has these records inside it.

Code:
SUB_ACC_NO|CLIENT_ID|ANUM|AKEY|ACCT_TYPE|FUND_ID|FUND_ABBR|TRADER_ID|TRADER_NAME|BROKER|VARIABLE_FIELDS_PRIMARY|CURRENCY|CLIENT_FUND_ID|ANAME|CASH_ANUM|ANUM_GROUP|VALUE_DATE|BALANCE|CUR_BALANCE|TRAN_STATUS|TRAN_TYPE|STMT_PG|SIDE|SIGN|REC_TYPE|BALANCE_TYPE|GIN2CXL|ETL_DATE

and there are other files that have the same header name but with different positions. what i wanted to achieve is to get the column number for the header field that starts with SUB_ACC_NO.

thanks
# 2  
Old 11-26-2013
NF is the number of fields in a line. You can walk across all of them to see if it equals SUB_ACC_NO like so:

Code:
$ awk -F\| '{ for (i=1;i<=NF;i++) if ($i == "SUB_ACC_NO") print i }' input
1

Since we only care about line 1 (where headers are), then use the condition NR==1 or you can exit early NR>1{exit}

Code:
awk -F\| 'NR>1{exit}1{ for (i=1;i<=NF;i++) if ($i == "SUB_ACC_NO") print i }' input

awk -F\| 'NR==1{ for (i=1;i<=NF;i++) if ($i == "SUB_ACC_NO") print i }' input

# 3  
Old 11-26-2013
Or more simply:
Code:
awk -F '|' '
{       for(i = 1; i <= NF; i++)
                if($i == "SUB_ACC_NO") {
                        print i
                        exit
                }
}' input

This User Gave Thanks to Don Cragun For This Post:
# 4  
Old 11-26-2013
Or simply

Code:
$ awk '$1 == "SUB_ACC_NO"{print NR;exit} ' RS="|" file

This User Gave Thanks to Akshay Hegde For This Post:
# 5  
Old 11-26-2013
Quote:
Originally Posted by Akshay Hegde
Or simply

Code:
$ awk '$1 == "SUB_ACC_NO"{print NR;exit} ' RS="|" file

Simply, but that's not the question, please reread the OP.
# 6  
Old 11-26-2013
Quote:
Originally Posted by Franklin52
Simply, but that's not the question, please reread the OP.

@Franklin52 I didn't get you. will you please explain me what went wrong..
# 7  
Old 11-26-2013
Quote:
Originally Posted by Akshay Hegde
@Franklin52 I didn't get you. will you please explain me what went wrong..
Sorry, I didn't noticed the RS in your solution.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk split columns to row after N number of column

I want to split this with every 5 or 50 depend on how much data the file will have. And remove the comma on the end Source file will have 001,0002,0003,004,005,0006,0007,007A,007B,007C,007E,007F,008A,008C Need Output from every 5 tab and remove the comma from end of each row ... (4 Replies)
Discussion started by: ranjancom2000
4 Replies

2. Shell Programming and Scripting

Count Repetitive Number in a column and renumbering using awk

Unable to get the desired output. Need only the rows which has repeated values in column 5. Input File <tab separated file> chr1 3773797 3773797 CEP10 1 chr1 3773797 3773797 CEP104 2 chr1 3689350 3689350 SMIM1 2 chr1 3773797 3773797 CEP4 3 chr1 3773797 3773797 EP104 ... (7 Replies)
Discussion started by: himanshu
7 Replies

3. Shell Programming and Scripting

awk to print column number while ignoring alpha characters

I have the following script that will print column 4 ("25") when column 1 contains "123". However, I need to ignore the alpha characters that are contained in the input file. If I were to ignore the characters my output would be column 3. What is the best way to print my column of interest... (3 Replies)
Discussion started by: ncwxpanther
3 Replies

4. Shell Programming and Scripting

awk - Print column number that return value comes from

I have the following awk script that I am using to find the max value in the file and print results. awk 'BEGIN {MAX=-1E100} {for (x=2; x<=NF; x++) if ($x>MAX) {MAX = $x; C1 = $1}} END {print substr(C1,1,11), substr(C1,13,4), substr(C1,18,2), MAX}' ABC* Input (ABC*) ... (6 Replies)
Discussion started by: ncwxpanther
6 Replies

5. Shell Programming and Scripting

Awk, appending a number in the first column of a row with a condition

Hi everyone, I have a data file in which the data is stored in event blocks. What I would like to get is that the same file with every data row starting with the number of event block. So here is two event blocks from my file: <event> -2 -1 0 0 0 501 0.00000000000E+00 ... (2 Replies)
Discussion started by: hayreter
2 Replies

6. UNIX for Dummies Questions & Answers

count number of distinct values in each column with awk

Hi ! input: A|B|C|D A|F|C|E A|B|I|C A|T|I|B As the title of the thread says, I would need to get: 1|3|2|4 I tried different variants of this command, but I don't manage to obtain what I need: gawk 'BEGIN{FS=OFS="|"}{for(i=1; i<=NF; i++) a++} END {for (b in a) print b}' input ... (2 Replies)
Discussion started by: beca123456
2 Replies

7. UNIX for Dummies Questions & Answers

how to count number of rows and sum of column using awk

Hi All, I have the following input which i want to process using AWK. Rows,NC,amount 1,1202,0.192387 2,1201,0.111111 3,1201,0.123456 i want the following output count of rows = 3 ,sum of amount = 0.426954 Many thanks (2 Replies)
Discussion started by: pistachio
2 Replies

8. UNIX for Dummies Questions & Answers

Adding a column with the row number using awk

Is there anyway to use awk to add a first column to my data that automatically goes from 1 to n , where n is the numbers of my rows?:confused: (4 Replies)
Discussion started by: cosmologist
4 Replies

9. Shell Programming and Scripting

column number, awk, help

All, $ cat myf.txt A|xyz|1000|mm B|9000|xyz|ss C|BDE|2000|kk D|xyz|1000|nn I am searching "xyz" $ awk -F "|" ' {for(k=0;k<=NF;k++) if ( $k == "xyz" ) print "line="NR"(column="k")" }' myf.txt Output: line=1(column=2) line=2(column=3) line=4(column=2) (2 Replies)
Discussion started by: jkl_jkl
2 Replies

10. Shell Programming and Scripting

awk to select a column from particular line number

The awk command awk -F: '{print $1}' test1 gives the first columns of all the lines in file ,is there some command to get a particular column from particular line . Any help is appreciated. thanks arif (4 Replies)
Discussion started by: mab_arif16
4 Replies
Login or Register to Ask a Question