Search for partial matches in particular column


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Search for partial matches in particular column
# 1  
Old 06-24-2015
Search for partial matches in particular column

I have a list

Code:
a
b
c
d

I want to search this list to have partial matches in column 2 in data file

Code:
col1	col2	col3
1	a/e	aa
2	b/e	aa
3	z/y	aa
4	t/u	bb
5	d/f	aa
6	a/t	aa

and extract the relevant rows with header

Code:
col1	col2	col3
1	a/e	aa
2	b/e	aa
5	d/f	aa
6	a/t	aa

What am I doing incorrectly here? Please assist.

Code:
awk  'NR==FNR{a[$1];next}{ for(i=1;i<=length(a);i++) if (($2~a[i]) || (NR==1) ){ print }}' list data

# 2  
Old 06-24-2015
Code:
cat $list|awk '{print $2}'|grep $value

# 3  
Old 06-24-2015
Why don't you tell us WHAT's going wrong in lieu of leaving it up to us to guess?

For the data file, NR can't be 1. User FNR instead.

---------- Post updated at 22:01 ---------- Previous update was at 21:58 ----------

@os2mac: the list file has one column, so awk '{print $2}' will print empty lines.
This User Gave Thanks to RudiC For This Post:
# 4  
Old 06-24-2015
Its going into an infinite loop , printing the header only repeatedly, using FNR also does the same thing.

Code:
col1    col2    col3
col1    col2    col3
col1    col2    col3
......

# 5  
Old 06-24-2015
Try
Code:
awk  '
NR==FNR         {a[$1]
                 next
                }
FNR==1
                {split ($2, T, "/")}
T[1] in a
' file1 file2
col1    col2    col3
1    a/e    aa
2    b/e    aa
5    d/f    aa
6    a/t    aa

This User Gave Thanks to RudiC 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 search for a word in column header that fully matches the word not partially in awk?

I have a multicolumn text file with header in the first row like this The headers are stored in an array called . which contains I want to search for each elements of this array from that multicolumn text file. And I am using this awk approach for ii in ${hdr} do gawk -vcol="$ii" -F... (1 Reply)
Discussion started by: Atta
1 Replies

2. UNIX for Beginners Questions & Answers

If pattern in column 3 matches pattern in column 2 (any row), print value in column 1

Hi all, I have searched and searched, but I have not found a solution that quite fits what I am trying to do. I have a long list of data in three columns. Below is a sample: 1,10,8 2,12,10 3,13,12 4,14,14 5,15,16 6,16,18 Please use code tags What I need to do is as follows: If a... (4 Replies)
Discussion started by: bleedingturnip
4 Replies

3. Shell Programming and Scripting

Duplicate identification using partial matches

Hi , I have a column with names. I would want to match names which match either completely or partially and capture them in separate column like below. Input Abc dbc abc xyz def bcd abc ggg xxx abc xxx Output| Duplicate Abc|abc xyz |abc ggg |xxx abc xxx... (1 Reply)
Discussion started by: ahmedwaseem2000
1 Replies

4. Shell Programming and Scripting

Difference of the same column when two other column matches and one column differs less than 1 hour

This is my input file : # cat list 20130430121600, cucm, location,76,2 20130430121600,cucm1,location1,76,4 20130430122000,cucm,location,80,8 20130430122000,cucm1,location1,90,8 20130430140000,cucm1,location1,87,11 20130430140000, cucm,location,67,9 This is the required output ... (1 Reply)
Discussion started by: Lakshmikumari
1 Replies

5. Shell Programming and Scripting

How to get difference of the same column between two files when other column matches?

File 1: 20130416,235800,10.78.25.104,BR2-loc,60.0,1624,50.0,0,50.0,0 20130416,235800,10.78.25.104,BR1-LOC,70.0,10,50.0,0,70.0,0 20130416,235800,10.78.25.104,Hub_None,60.0,15,60.0,0,50.0,0 File 2: 20130417,000200,10.78.25.104,BR2-loc,60.0,1626,50.0,0,50.0,0... (3 Replies)
Discussion started by: Lakshmikumari
3 Replies

6. Shell Programming and Scripting

awk - how to get difference of the same column when other column matches

I have a file like this : # cat list cucm, location,76,2 cucm1,location1,76,4 cucm,location,80,8 cucm1,location1,90,8 cucm1,location1,87,11 cucm,location,67,9 and I want output like this : cucm,location,76,2 cucm1,location1,76,4 cucm,location,80, 6 ===> (8-2 =6) cucm1,location1,90,4... (5 Replies)
Discussion started by: Lakshmikumari
5 Replies

7. Shell Programming and Scripting

Replace column that matches specific pattern, with column data from another file

Can anyone please help with this? I have 2 files as given below. If 2nd column of file1 has pattern foo1@a, find the matching 1st column in file2 & replace 2nd column of file1 with file2's value. file1 abc_1 foo1@a .... abc_1 soo2@a ... def_2 soo2@a .... def_2 foo1@a ........ (7 Replies)
Discussion started by: prashali
7 Replies

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

9. Shell Programming and Scripting

Using grep returns partial matches, I need to get an exact match or nothing

I’m trying to modify someone perl script to fix a bug. The piece of code checks that the zone name you want to add is unique. However, when the code runs, it finds a partial match using grep, and decides it already exists, so the “create” command exits. $cstatus = `${ZADM} list -vic | grep... (3 Replies)
Discussion started by: TKD
3 Replies

10. Shell Programming and Scripting

Partial average of a column with awk

Hello, Let's assume I have 100 files FILE_${m} (0<m<101). Each of them contains 100 lines and 10 columns. I'd like to get in a file called "result" the average value of column 3, ONLY between lines 11 and 17, in order to plot that average as a function of the parameter m. So far I can compute... (6 Replies)
Discussion started by: DMini
6 Replies
Login or Register to Ask a Question