pattern matching with comma delimited text


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users pattern matching with comma delimited text
# 1  
Old 12-11-2008
pattern matching with comma delimited text

Hi,

I have two files that I need to match patterns with and the second file has comma delimited rows of data that match but I'm having trouble getting a script to work that gives me the match output to these sets :

file 1:

PADG_05255
PADG_06803
PADG_07148
PADG_02849
PADG_02886
PADG_05295
PADG_01010
PADG_01141
PADG_02836
PADG_04289
PADG_08126
PADG_05265
PADG_00557
PADG_05676
PADG_03527
PADG_00102
PADG_07457
PADG_03012
PADG_05018
PADG_04985
PADG_07697


file 2:

PADG_04295
PADG_04294
PADG_04293
PADG_01010,PADG_04292
PADG_04291
PADG_04289
PADG_04288
PADG_04287,PADG_02886,PADG_07699
PADG_04286
PADG_04285
PADG_04284
PADG_04283
PADG_04281
PADG_03285
PADG_04280
PADG_04279
PADG_04278


Desired output:

PADG_01010,PADG_04292
PADG_04287,PADG_02886,PADG_07699
PADG_04289
# 2  
Old 12-11-2008
there're multiple threads on a similar subject - pls use the 'Search' function to find a 'hint' (not THE solution).
Code:
nawk -F',' 'NR==FNR {a[$0]; next} $1 in a' file1 file2

# 3  
Old 12-11-2008
Code:
grep -f file1  file2 > results

# 4  
Old 12-11-2008
Thanks for your help!

I don't have nawk- but, using awk I got the following output:
PADG_04289

Any suggestions on how to get the output:
PADG_01010,PADG_04292
PADG_04287,PADG_02886,PADG_07699
PADG_04289

(the comma delimited text is really giving me the problem)
Thanks again,
M
# 5  
Old 12-11-2008
given your sample input files, I get this:
Code:
PADG_01010,PADG_04292
PADG_04289

'PADG_04287' ( in file2's 'PADG_04287,PADG_02886,PADG_07699') is NOT in file1. Where does it come from?
# 6  
Old 12-11-2008
PADG_04287,PADG_02886,PADG_07699 should be in the output because PADG_02886 is
in files 1 and 2.
# 7  
Old 12-11-2008
Code:
nawk -F',' 'NR==FNR {a[$0]; next} {for(i=1;i<=NF;i++) if ($i in a) {print;break}}' file1 file2

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Need help on an old post - How to convert a comma delimited string to records or lines of text?

Hi, Apologies in advance to the moderator if I am posting this the wrong way. I've searched and found the solution to an old post but as it is a very old post, I don't see an option to update it with additional question. The question I have is in relation to the following post: How to... (6 Replies)
Discussion started by: newbie_01
6 Replies

2. Shell Programming and Scripting

Help/Advise please for converting space delimited string variable to comma delimited with quote

Hi, I am wanting to create a script that will construct a SQL statement based on a a space delimited string that it read from a config file. Example of the SQL will be For example, it will read a string like "AAA BBB CCC" and assign to a variable named IN_STRING. I then concatenate... (2 Replies)
Discussion started by: newbie_01
2 Replies

3. UNIX for Dummies Questions & Answers

How to convert a comma delimited string to records or lines of text?

Hi, I am not sure if I've posted this question before. Anyway, I previously asked about converting lines of text into a comma delimited string. Now I am needing to do the other way around ... :( :o Can anyone advise how is this possible? Example as below: Converting records/lines to... (2 Replies)
Discussion started by: newbie_01
2 Replies

4. UNIX for Dummies Questions & Answers

How to change a line of text to a comma delimited string?

Hi, Is there a one-liner that I can use to change a line of text into a comma delimited string? For example, convert user1 user2 user3 user4to user1,user2,user3,user4Currently using while read x, although got the extra comma at the end that I have to remove manually. Please... (5 Replies)
Discussion started by: newbie_01
5 Replies

5. Shell Programming and Scripting

Need a script to convert comma delimited files to semi colon delimited

Hi All, I need a unix script to convert .csv files to .skv files (changing a comma delimited file to a semi colon delimited file). I am a unix newbie and so don't know where to start. The script will be scheduled using cron and needs to convert each .csv file in a particular folder to a .skv... (4 Replies)
Discussion started by: CarpKing
4 Replies

6. Shell Programming and Scripting

Pattern Matching and text deletion using VI

Can someone please assist me, I'm trying to get vi to remove all the occurences of the text in a file i.e. "DEVICE=/dev/mt??". The "??" represents a number variable. Is there a globel search and delete command that I can use? Thank You in Advance. (3 Replies)
Discussion started by: roadrunner
3 Replies

7. Shell Programming and Scripting

text manipulation and pattern matching

Hi guys, I need help: I started receiving automatic emails containing download information. The problem is that these emails are coming in a rich format (I have no control of this) so the important information is buried under a bunch of mumbo-jumbo. To complicated things even further I need to... (10 Replies)
Discussion started by: caprica13
10 Replies

8. Shell Programming and Scripting

How do you delete multiple text from a comma delimited file

I would like to know code that will delete multiple text from a comma delimited file. For example, how would the comma delimited file below delete the word 'PEST' in Perl language (previously an excel file that was converted to a csv and the last column was PEST): 1, 2,43,34, bosx,PEST 1,... (1 Reply)
Discussion started by: dolo21taf
1 Replies

9. Shell Programming and Scripting

Delete a block of text delimited by blank lines when pattern is found

I have a file which contains blocks of text - each block is a multi-lines text delimited by blank lines eg. <blank line> several lines of text ... pattern found on this line several more lines of text ... <blank line> How do you delete the block of text (including the blank lines) when... (17 Replies)
Discussion started by: gleu
17 Replies

10. Shell Programming and Scripting

Parsing comma delimited text file

I need to delete a set of files in certain directories if there're older than a certain number of days. So I have a text file, with each line containing the directory & number of days. The format is like this: dirA,5 dirB,7 How do I write script to iteratively parse this text file & delete... (5 Replies)
Discussion started by: chengwei
5 Replies
Login or Register to Ask a Question