awk match second column


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk match second column
# 1  
Old 03-16-2010
awk match second column

Hi I have file which looking like:

Code:
fox_spectrum	fox_spectrum\
fox_spectrum	(fox_spectrum)\

I just want lines which did not begin with brackets () in second column

I tried:
Code:
awk -v var='^(.*' '{if ($2!=var) print $0}' file

but it returns whole file, thanks a lot
# 2  
Old 03-16-2010
Try:
Code:
awk '$2 !~ /\(/' file

# 3  
Old 03-16-2010
Thanks, that works Smilie
Is possible to do same thing but if file would looks like this (containing line bellow)

Code:
fox_spectrum	fox_spectrum\
20
fox_spectrum	(fox_spectrum)\
15

In other word filter all second columns that did not contain ( at begging and also line bellow ?

so result would be:

Code:
fox_spectrum	fox_spectrum\
20

Also could you please explain why is my first experiment wrong ? I assuming is not possible to asign regular expression to variable.Thanks
# 4  
Old 03-16-2010
Quote:
Originally Posted by wakatana
Thanks, that works Smilie
Is possible to do same thing but if file would looks like this (containing line bellow)

Code:
fox_spectrum	fox_spectrum\
20
fox_spectrum	(fox_spectrum)\
15

In other word filter all second columns that did not contain ( at begging and also line bellow ?

so result would be:

Code:
fox_spectrum	fox_spectrum\
20

Try this:
Code:
awk '$2 !~ /^\(/{print;getline;print}' file

Quote:
Originally Posted by wakatana
Also could you please explain why is my first experiment wrong ? I assuming is not possible to asign regular expression to variable.Thanks
With a variable:
Code:
awk -v var="^\(.*" '$2 !~ var' file

# 5  
Old 03-16-2010
If you are comparing with string then use =
If you are comparing with regular expression then use ~

Code:
$ awk -v var='^\\(.*'  ' {if ($2!~var) print $0}' file
fox_spectrum    fox_spectrum\
20
15

# 6  
Old 03-16-2010
In the blank before the "(" is a literal tab.
Code:
sed '/   (/{N;d;}' file

Cheers,
Alister
# 7  
Old 03-17-2010
anbu23: thanks for explanation I saw somewhere = and somewhere ~ and gives me no sense Smilie now it is clear, just one question why did you escaped string with \\ is it something with bash or what ?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Conversion if 1st column is match (awk '{s+=$1} END

Hi im trying to add numbers, got no problem with it im using awk '{s+=$1} END {print s, "MB"}', but what if the numbers are like mention below. who will i add them 2000 KB 1 MB Answer: 2001 Desired: 2000 KB 1 MB Answer: 3000 (4 Replies)
Discussion started by: invinzin21
4 Replies

2. Shell Programming and Scripting

awk Match First Field and Replace Second Column

Hi Friends, I have looked around the forums and over online but couldn't figure out how to deal with this problem input.txt gene1,axis1/0/1,axis2/0/1 gene1,axis1/1/2,axis2/1/2 gene1,axis1/2/3,axis2/2/3 gene2,axis1/3/4,axis2/3/4 Match on first column and if first column is... (1 Reply)
Discussion started by: jacobs.smith
1 Replies

3. Shell Programming and Scripting

awk Print New Column For Every Two Lines and Match On Multiple Column Values to print another column

Hi, My input files is like this axis1 0 1 10 axis2 0 1 5 axis1 1 2 -4 axis2 2 3 -3 axis1 3 4 5 axis2 3 4 -1 axis1 4 5 -6 axis2 4 5 1 Now, these are my following tasks 1. Print a first column for every two rows that has the same value followed by a string. 2. Match on the... (3 Replies)
Discussion started by: jacobs.smith
3 Replies

4. Shell Programming and Scripting

awk pattern match and count unique in column

Hi all I have a need of searching some pattern in file by month and then count unique records D11 G11 R11 -------> Pattern available in file S11 Jan$1 to $5 column contains some records in which I want to find unique for this purpose I have written script like below awk '/Jan/ ||... (4 Replies)
Discussion started by: nex_asp
4 Replies

5. Shell Programming and Scripting

awk strings search + print next column after match

Hi, I have a file filled with search strings which have a blank in between and look like this: S. g. Ehr. o. Jg. v. d. Chijs g. Ehr. Now i would like to search for the strings and it also shall return the next column after the match. awk -v FILE="search_strings.txt" 'BEGIN {... (10 Replies)
Discussion started by: sdf
10 Replies

6. Shell Programming and Scripting

awk/sed to extract column bases on partial match

Hi I have a log file which has outputs like the one below conn=24,196 op=1 RESULT err=0 tag=0 nentries=9 etime=3,712 dbtime=0 mem=486,183,328/2,147,483,648 Now most of the time I am only interested in the time ( the first column) and a column that begins with etime i.e... (8 Replies)
Discussion started by: pkabali
8 Replies

7. Shell Programming and Scripting

Awk or Sed, fubd match in column, then edit column.

FILE A: 9780743551526,(Abridged) 9780743551779,(Unabridged) 9780743582469,(Abridged) 9780743582483,(Unabridged) 9780743563468,(Abridged) 9780743563475,(Unabridged) FILE B: c3saCandyland 9780743518321 "CANDYLAND" "MCBAIN, ED" 2001 c3sbCandyland 9780743518321 ... (7 Replies)
Discussion started by: glev2005
7 Replies

8. Shell Programming and Scripting

Match column 3 in file1 to column 1 in file 2 and replace with column 2 from file2

Match column 3 in file1 to column 1 in file 2 and replace with column 2 from file2 file 1 sample SNDK 80004C101 AT XLNX 983919101 BB NETL 64118B100 BS AMD 007903107 CC KLAC 482480100 DC TER 880770102 KATS ATHR 04743P108 KATS... (7 Replies)
Discussion started by: rydz00
7 Replies

9. Shell Programming and Scripting

Question on awk for finding the column number using a match word

Hi Guys, Please help me out in my situation of writing a shell script Exampl:I have a output like asnapply 1 2 3 apply_server=1 apply_schema=ASN asnapply 1 2 3 apply_server=2 apply_schema=ASN Now i need output like asnacmd applysever=1 applyschema=ASN stop asnacmd applysever=2... (16 Replies)
Discussion started by: mallak
16 Replies

10. Shell Programming and Scripting

Awk+Grep Input file needs to match a column and print the entire line

I'm having problems since few days ago, and i'm not able to make it works with a simple awk+grep script (or other way to do this). For example, i have a input file1.txt: cat inputfile1.txt 218299910417 1172051195 1172070231 1172073514 1183135117 1183135118 1183135119 1281440202 ... (3 Replies)
Discussion started by: poliver
3 Replies
Login or Register to Ask a Question