column number, awk, help


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting column number, awk, help
# 1  
Old 03-15-2008
column number, awk, help

All,

Code:
$ 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)


I want to see the output as :

line=1(column=2)
line=2(column=3)
line=3(Not found)
line=4(column=2)

HTH,
jkl_jkl
# 2  
Old 03-15-2008
It should be like:

Code:
awk -F "|" '
{found=0}
{for(k=0;k<=NF;k++)
   if( $k == "xyz" ){
     print "line="NR"(column="k")"
     found=1
   }
}
!found{print "line="NR" (not found)"}
' myf

Regards
# 3  
Old 03-15-2008
Check this way:
Code:
> cat file
A|xyz|1000|mm
B|9000|xyz|ss
C|BDE|2000|kk
D|xyz|1000|nn

Code:
>  awk '{printf("%s","line="NR);a=gsub(/\|/,"",$1);if (a==3){print "(Not Found)";next}print "(column="a+1")"}' FS='xyz' file
line=1(column=2)
line=2(column=3)
line=3(Not Found)
line=4(column=2)

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. Shell Programming and Scripting

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. ... (7 Replies)
Discussion started by: reignangel2003
7 Replies

7. 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

8. 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

9. 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

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