awk pattern matching problem -not recognizing a column


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk pattern matching problem -not recognizing a column
# 1  
Old 06-18-2010
Data awk pattern matching problem -not recognizing a column

Hi all,
I am new to awk.

I want to print the line numbers if the column has a particular value.

For example I have:
Code:
cat FILE1
COL1 COL2
X114 0
X116 0
X117 0
X120 0
X121 0
X125 0
X126 0
X127 0
X131 1
X132 0
X135 0
X136 0
X137 0
X138 0
X140 0
X141 0
X143 0
X144 0
X146 0
X147 1
X150 0

I want to print the lines having value 1 in column 2.
My output should be
Code:
10
21

I tried:
Code:
awk -v a=2 -F '\t' 'BEGIN {x=1}; {if (1~$a) print x; x++}' FILE1

But it didnot produce any output.

After trying for a long time, I just added aother column to this file (in excel) with zeros. ie;
Code:
cat FILE2
COL1 COL2 0
X114 0 0
X116 0 0
X117 0 0
X120 0 0
X121 0 0
X125 0 0
X126 0 0
X127 0 0
X131 1 0
X132 0 0
X135 0 0
X136 0 0
X137 0 0
X138 0 0
X140 0 0
X141 0 0
X143 0 0
X144 0 0
X146 0 0
X147 1 0
X150 0 0
X152 0 0

Now when I do:
Code:
awk -v a=2 -F '\t' 'BEGIN {x=1}; {if (1~$a) print x; x++}' FILE2

I am getting the correct output.

can anyone help me on why awk is not working for FILE1. I have to do the same in 100 files. I am not sure what mistake I had made when working on FILE1 format

Your reply will be very helpful. Thanks in advance

Last edited by Franklin52; 06-18-2010 at 01:47 PM.. Reason: Please use code tags
# 2  
Old 06-18-2010
try:

Code:
#  awk '$2==1{print NR}' file1
10
21

HTH
This User Gave Thanks to Tytalus For This Post:
# 3  
Old 06-18-2010
Hi what happens if you leave out:
Code:
-F '\t'

If you specify the separation characters, then each tab separates a different field, so to consecutive tabs means one empty field. If you leave out the -F option multiple tabs or spaces or mixture thereof gets interpreted as one separator. If you are exporting from excel, perhaps this was not done consistently, or perhaps is was space separated he first time?
# 4  
Old 06-18-2010
alternative Smilie
Code:
# cat colon
COL1 COL2
X114 0
X116 0
X117 0
X120 0
X121 0
X125 0
X126 0
X127 0
X131 1
.........

Code:
# sed -n '/ 1/=' colon
10
21

This User Gave Thanks to ygemici For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Matching column value from 2 different file using awk and append value from different column

Hi, I have 2 csv files. a.csv HUAWEI,20LMG011_DEKET_1296_RTN-980_IDU-1-11-ISV3-1(to LAMONGAN_M),East_Java,20LMG011_DEKET_1296_RTN-980_IDU-1,20LMG011,20LMG 027_1287_LAMONGAN_RTN980_IDU1,20LMG027,1+1(HSB),195.675,20LMG011-20LMG027,99.9995,202.6952012... (7 Replies)
Discussion started by: tententen
7 Replies

2. Shell Programming and Scripting

Pattern matching problem

if i have to do pattern match for file name with digit alphanumeric value like this File_1234.csv File_12sd45rg.csv i am using this File_*.csv and File_*.csv for digit pattern match. when i am doing pattern match for the digit then both alphanumeric match and digit match is coming. ... (3 Replies)
Discussion started by: ramsavi
3 Replies

3. Shell Programming and Scripting

Sum of column matching pattern/string

Hi All, I have the following output file created as a result of one script called pattern_daily.log $ cat pattern_daily.log Approved|2|Wed, Oct 24, 2012 11:21:09 AM Declined|1|Wed, Oct 24, 2012 11:21:15 AM Approved|2|Wed, Oct 24, 2012 11:24:08 AM Declined|1|Wed, Oct 24, 2012 11:24:18 AM... (4 Replies)
Discussion started by: Gem_In_I
4 Replies

4. Shell Programming and Scripting

Pattern matching problem

Hi I need a bash script that can search through a text file for all lines starting with 71502FSC1206 on every line it finds starting with this I need to place a letter F at the 127 position on that line. Thanks Paul (6 Replies)
Discussion started by: firefox2k2
6 Replies

5. Programming

pl sql . pattern matching problem

hi everyone i am facing a strange problem declare v_var number(10); begin if( regexp_like('RCDORMS_MMS_*_DAR','RCDORMS_MMS_*_DAR')) then v_var:=20; dbms_output.put_line(v_var); end if; end; / please tell me what's the wrong thing in this expression.. as i am not able to get... (1 Reply)
Discussion started by: aishsimplesweet
1 Replies

6. Shell Programming and Scripting

pattern matching problem

# cat email.txt | grep -i "To:" To: <test@example.com> # cat email.txt | grep -i "Subject" Subject: Test Subject: How are you. I need to print only test@example.com from To field need to eliminate "< & >" from To field and need to print entire subject after Subject: It should be #... (7 Replies)
Discussion started by: mirfan
7 Replies

7. Shell Programming and Scripting

problem using sed for pattern matching

if abc.sh is 192.168.1.41 then the output that i get is v5c01 my code is sed 's/192.168.1.4/v5c0/g s/192.168.1.41/acc1/g' abc.sh 2>&1 | tee abc.sh i want to find 192.168.1.4 and replace it with v5c0 and find 192.168.1.41 and replace it with acc1 and i want to do it using sed (5 Replies)
Discussion started by: lassimanji
5 Replies

8. Shell Programming and Scripting

pattern matching problem

FilesToBackup='*.track* *.xml *.vm* *.gz Trace* TRACE* "*core*" *.out fcif_data_* esi_error_* *.rollback *.sed R.* APStatus_* log* *.output* send_mail* downenv* check_env* intaspurge_db_* sqlnet.log *.rpt *.html *.csv "*TSC*"' and i am using it like this- echo Moving files from $(pwd): ... (2 Replies)
Discussion started by: namishtiwari
2 Replies

9. Shell Programming and Scripting

problem with CASE pattern matching

I am using ksh on a HP Ux. I have a simple script but am having problem with the case statement:- #!/usr/bin/sh Chl=”SM.APPLE_SWIFT_DV” LoConfirm=”” case $chl in ) LoConfirm=”Using channel at Building 1” echo “test conditon1” echo $LoConfirm;; ) LoConfirm=”Using... (2 Replies)
Discussion started by: gummysweets
2 Replies

10. Shell Programming and Scripting

pattern matching problem

I have a file with the following contents; NEW 85174 MP081 /29OCT07 CNL 85986 MP098 /28OCT07 NEW 86014 MP098 /28OCT07 NEW 86051 MP097 /27OCT07 CNL 86084 MP097 /27OCT07 Now I have to retrieve all lines that start with NEW and where the next line starts with CNL and where the MP codes are... (8 Replies)
Discussion started by: rein
8 Replies
Login or Register to Ask a Question