awk script - need help finding matching entries


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk script - need help finding matching entries
# 1  
Old 03-13-2013
IBM awk script - need help finding matching entries

I have a config file like below
Code:
JOHN    1,5,6,7,4,999,222,666,555
KELLY    54,77,33
ABC    98,22,11

name and then comma separated unique values. In my code I will have a number eg: 6 and I want to find out matching name eg: JOHN
How do i do ? Please advise. Thanks.

Last edited by joeyg; 03-13-2013 at 09:59 AM.. Reason: Please use code tags for data and code samples
# 2  
Old 03-13-2013
Code:
root# x=6; awk '$2 ~ /^'$x',|,'$x',|,'$x'$/ {print $1}' file

This User Gave Thanks to balajesuri For This Post:
# 3  
Old 03-13-2013
one simple solution would be

Code:
awk '$2 ~ "6" { print $1 }' filename

# 4  
Old 03-13-2013
This will give all match where it is 16 66 6 etc...
I need exact match.

---------- Post updated at 06:04 AM ---------- Previous update was at 06:00 AM ----------

Quote:
Originally Posted by balajesuri
Code:
root# x=6; awk '$2 ~ /^'$x',|,'$x',|,'$x'$/ {print $1}' file

Thanks! it worked Smilie
Can you please explain how it is actually working? logic behind this? Thanks a lot.
# 5  
Old 03-13-2013
In field 2, match 6 if it is at the beginning (^'$x') or if it is in the middle (,'$x',) or if it is in the end ('$x'$), and print corresponding $1.

Note: Here, $x is interpreted by shell and the its value is used in awk, and hence the quotes around it.
This User Gave Thanks to balajesuri For This Post:
# 6  
Old 03-13-2013
Got it! Thanks a lot Smilie Now I understand logic behind it.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk matching script not working as expected

This is my ubuntu version: $ lsb_release -a No LSB modules are available. Distributor ID: Ubuntu Description: Ubuntu 16.04.4 LTS Release: 16.04 Codename: xenial $ /bin/awk -V | head -n1 bash: /bin/awk: No such file or directory I have gotten a script that helps me to parse,... (14 Replies)
Discussion started by: delbroooks
14 Replies

2. Shell Programming and Scripting

awk to combine all matching dates and remove non-matching

Using the awk below I am able to combine all the matching dates in $1, but I can not seem to remove the non-matching from the file. Thank you :). file 20161109104500.0+0000,x,5631 20161109104500.0+0000,y,2 20161109104500.0+0000,z,2 20161109104500.0+0000,a,4117... (3 Replies)
Discussion started by: cmccabe
3 Replies

3. Shell Programming and Scripting

Finding matching patterns in two files

Hi, I have requirement to find the matching patterns of two files in Unix. One file is the log file and the other is the error list file. If any pattern in the log file matches the list of errors in the error list file, then I would need to find the counts of the match. For example, ... (5 Replies)
Discussion started by: Bobby_2000
5 Replies

4. Shell Programming and Scripting

awk script issue redirecting to multiple files after matching pattern

Hi All I am having one awk and sed requirement for the below problem. I tried multiple options in my sed or awk and right output is not coming out. Problem Description ############################################################### I am having a big file say file having repeated... (4 Replies)
Discussion started by: kshitij
4 Replies

5. Programming

awk script for finding probability of distribution of numbers

Dear All I am having data file containing 0 to 40,000 like this... 0 5 1 65 2 159 3 356 ... ... 40000 19 I want to find the probability of distribution between the numbers. The second column values are angles from 0 to 360 and the 1st column is number of files. I am expecting... (2 Replies)
Discussion started by: bala06
2 Replies

6. Shell Programming and Scripting

Awk - Script assistance on identifying non matching fields

Hoping for some assistance. my source file consists of: os, ip, username win7, 123.56.78, john win7, 123.56.78, paul win7, 10.1.1.1, john win7, 10.2.2.3, joe I've been trying to run a script that will only return ip and username where the IP address is the same and the username is... (3 Replies)
Discussion started by: tekvaio
3 Replies

7. UNIX for Dummies Questions & Answers

Help with finding matching position on strings

I have a DNA file like below and I am able to write a short program which finds/not an input motif, but I dont understand how I can include in the code to report which position the motif was found. Example I want to find the first or all "GAT" motifs and want the program to report which position... (12 Replies)
Discussion started by: pawannoel
12 Replies

8. UNIX for Dummies Questions & Answers

Matching pattern script (sed or awk?)

Hi Guys, I am new to the forum and to scripting so bear with me. Thanks, Gary. I have 3 files - file1, file2, file3 I am trying to come up with a script that will check the output of these files and if the 1st nine fields are matched in all 3 files, echo "The following string had been... (2 Replies)
Discussion started by: gazza-o
2 Replies

9. Shell Programming and Scripting

Matching same columns and finding the smallest match

Hi all, I am wondering if its possible to solve my problem with a simple code. Basically I have a file that looks like this (tab delimited) bob 8 250 tina 8 225 sam 8 225 ellen 9 315 kyle 9 275 sally 9 135 So what I want to do is match columns 2 and 5. If columns 2 and 5... (2 Replies)
Discussion started by: phil_heath
2 Replies

10. Shell Programming and Scripting

finding duplicate files by size and finding pattern matching and its count

Hi, I have a challenging task,in which i have to find the duplicate files by its name and size,then i need to take anyone of the file.Then i need to open the file and find for more than one pattern and count of that pattern. Note:These are the samples of two files,but i can have more... (2 Replies)
Discussion started by: jerome Sukumar
2 Replies
Login or Register to Ask a Question