Compare 2 lists using a full and/or partial match at beginning of line?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Compare 2 lists using a full and/or partial match at beginning of line?
# 1  
Old 10-06-2010
Compare 2 lists using a full and/or partial match at beginning of line?

hello all,

I wonder if anybody might be able to help with this.

I have file 1 and file2.
Both files may contain thousands of lines that have variable contents.

file1

234GH
5234BTW
89er
678tfg
234
234YT
tfg456
wert
78gt
gh23444


file2

234
tfg
gh23
BTW



how can search file1 for the items that are in file2, and output the result to file3?

I would only want exact matches and also any lines that began with the items in file2.


so in the above case i would want to see



file3

234GH
tfg456
234
234YT
gh23444


example - i search for 234 and it finds three lines, an exact match and 2 lines that begins with 234.

example - i search for tfg and it finds one line that begins with tfg.

example - i search for gh23 and it finds one line that begins with gh23

Hope this makes sense Smilie

Cheers
Garred
# 2  
Old 10-06-2010
Code:
$ ruby -ne 'BEGIN{f1=File.read("file1").split}; print "#{$_}\n" if f1.include?($_.chomp!);END{}' file

# 3  
Old 10-06-2010
Code:
sed 's/^/^/' file2 | grep -f- file1 > file3

# 4  
Old 10-06-2010
Code:
 xargs -i grep ^{} file1 < file2

# 5  
Old 10-08-2010
thanks for all the replies, it's appreciated Smilie

kurumi - it is solaris that i am using and it does not have ruby installed Smilie


scrutinizer - i get the followong when i try your suggestion ...

sed 's/^/^/' file2 | grep -f- file1
BTWsed: Command not found
grep: illegal option -- f
grep: illegal option -- -
Usage: grep -hblcnsviw pattern file . . .


rdcwayx - doe s not produce the output i desire, and seems to duplicate items ...

xargs -i grep ^{} file1 < file2
234GH
5234BTW
89er
678tfg
234
234YT
tfg456
wert
78gt
gh23444
234GH
5234BTW
89er
678tfg
234
234YT
tfg456
wert
78gt
gh23444
234GH
5234BTW
89er
678tfg
234
234YT
tfg456
wert
78gt
gh23444
234GH
5234BTW
89er
678tfg
234
234YT
tfg456
wert
78gt
gh23444

Apologies if i have misunderstood any of you, newbie here so its all new to me.

Thanks
# 6  
Old 10-08-2010
Hi, I am guessing you are on Solaris. Try this:
Code:
/usr/xpg4/bin/sed 's/^/^/' file2 | /usr/xpg4/bin/grep -f- file1

# 7  
Old 10-12-2010
Quote:
Originally Posted by Scrutinizer
Hi, I am guessing you are on Solaris. Try this:
Code:
/usr/xpg4/bin/sed 's/^/^/' file2 | /usr/xpg4/bin/grep -f- file1

hi, thanks for the response Smilie

I am using Solaris, SunOS 5.8.

When trying your suggestion I get the following error message:

/usr/xpg4/bin/sed 's/^/^/' file2 | /usr/xpg4/bin/grep -f- file1
grep: can't open "-"

Smilie

Cheers
Garred
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Rename file using partial match to another

In the below I am trying to rename the contents within each data subfolder in a specific run, based on a partial match of the IonCode_0000_ in each file in the data subdirectory to $1 in f1. There will be multiple runs in f1 but each run in $uniq is unique and will be found in f1 and the rename... (27 Replies)
Discussion started by: cmccabe
27 Replies

2. UNIX for Beginners Questions & Answers

Inserting n characters to beginning of line if match

I would like to insert n number of characters at the beginning of each line that starts with a given character. If possible, I would be most appreciative for a sed or awk solution. Given the data below, I would like to be able to insert either 125 spaces or 125 "-" at the beginning of every line... (6 Replies)
Discussion started by: jvoot
6 Replies

3. Shell Programming and Scripting

awk to update file based on partial match in field1 and exact match in field2

I am trying to create a cronjob that will run on startup that will look at a list.txt file to see if there is a later version of a database using database.txt as the source. The matching lines are written to output. $1 in database.txt will be in list.txt as a partial match. $2 of database.txt... (2 Replies)
Discussion started by: cmccabe
2 Replies

4. Shell Programming and Scripting

Partial Match and Replace

Hi, I have a tab delimited text file like this one. I need to do a partial match of a particular cell and then replace matches with an empty cell. So here is a sample: Smith FordMustang ChevroletCamaro Miller FordFiesta Jones KiaSorrento Davis ChevroletCamaro Johnson ToyotaHighlander I... (4 Replies)
Discussion started by: mikey11415
4 Replies

5. Shell Programming and Scripting

Match partial text

I posted the incorrect files yesterday and apologize. I also modified the awk script but with no luck. There are two text files in the zip (name.txt and output.txt). I am trying to match $2 in name.txt with $1 in output.txt and if they match then $1 of name.txt is copied to $7 of output.txt. ... (7 Replies)
Discussion started by: cmccabe
7 Replies

6. UNIX for Dummies Questions & Answers

How to substitute for the partial match?

Hi I have a question and hope I can get answer here. Thank you in advance. I have two files: file1: aa X bb Y cc Z file2: cc A bb B dd C aa D bb E If the 1st column match in both file1 and file2, the 2nd column in file2 will be replaced by the 2nd column in file1. If there is no... (2 Replies)
Discussion started by: yuejian
2 Replies

7. UNIX for Dummies Questions & Answers

Partial match in two files then substitute

Hi, I was trying to figure this out but failed so I hope someone here can help me, thank you in advance. I have two files. file1: aa M bb N cc O dd P ee Q file2: aa A_87_P254063 cc A_87_P016532 bb A_87_P104793 dd A_87_P055331 ee A_87_P059706 aa A_87_P071636 ee A_87_P028302... (2 Replies)
Discussion started by: yuejian
2 Replies

8. UNIX for Dummies Questions & Answers

How to match on phrase at beginning of line and specific columns?

Here is my file: 700 7912345678910 61234567891234567891 700 8012345678910 61234567891234567891 I want to pull all lines that begin with '700' only if columns 11-12 are '79'. My code so far only pulls the '79', not the whole line: grep ^700 file1 | cut -c 11,12 |... (7 Replies)
Discussion started by: Scottie1954
7 Replies

9. Shell Programming and Scripting

AWK - Print partial line/partial field

Hello, this is probably a simple request but I've been toying with it for a while. I have a large list of devices and commands that were run with a script, now I have lines such as: a-router-hostname-C#show ver I want to print everything up to (and excluding) the # and everything after it... (3 Replies)
Discussion started by: ippy98
3 Replies

10. Shell Programming and Scripting

awk partial match and filter records

Hi, I am having file which contains around 15 columns, i need to fetch column 3,12,14 based on the condition that column 3 starts with 40464 this is the sample data how to achieve that (3 Replies)
Discussion started by: aemunathan
3 Replies
Login or Register to Ask a Question