searching string from one file and check it in the other file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting searching string from one file and check it in the other file
# 1  
Old 06-20-2011
searching string from one file and check it in the other file

Hello, Could you please help me in the script

I have two files:

FileOne
has the below data:
Code:
000004
500000
111234
123456

##################
FileTwo
has the below data:
Code:
434556          New York
545333          Paris
434344          Paris
111234          Berlin
434322          Boston
123456          Warsaw
443434          London
000334          Tokyo

##################

I have to check data from FileOne and check line after line if exist in FileTwo, if it exists print data from FileTwo

in this case, I should have:
Code:
111234                   Berlin
123456                   Warsaw


My idea is to do as below:

Code:
for qwerty in `awk ' { print $1 }' FileOne`     
do
  cat FileTwo | grep $qwerty > FinalFile
  cat FinalFile
done


But it doesn't work...
Whats your idea?

Last edited by Franklin52; 06-20-2011 at 05:27 PM.. Reason: Please indent your code and use code tags
# 2  
Old 06-20-2011
Code:
egrep -f file_one file_two

This User Gave Thanks to Shell_Life For This Post:
# 3  
Old 06-20-2011
Quote:
Originally Posted by Shell_Life
Code:
egrep -f file_one file_two

Good solution, but egrep is a bit overkill. Even basic regular expressions aren't required. It's probably safest and swiftest to use fixed string matching. I'd suggest:
Code:
grep -Ff file_one file_two

Regards,
Alister
This User Gave Thanks to alister For This Post:
# 4  
Old 06-20-2011
In case the first field doesn't have a fixed length and may encouter subpattern matching

To enhance the matching (if your implementation support the standard output "-" notation) :

Code:
sed 's/.*/^& /' file_one | egrep -f - file_two



Code:
sed 's/.*/^& /' file_one | grep -Ff - file_two

This User Gave Thanks to ctsgnb For This Post:
# 5  
Old 06-20-2011
Quote:
Originally Posted by ctsgnb
In case the first field doesn't have a fixed length and may encouter subpattern matching

To enhance the matching (if your implementation support the standard output "-" notation) :

Code:
sed 's/.*/^& /' file_one | egrep -f - file_two



Code:
sed 's/.*/^& /' file_one | grep -Ff - file_two

That last suggestion won't work. grep will try to match a literal ^ so long as -F is used.

However, you bring up a good point regarding substrings being matched and it's something to keep in mind in case the real data deviates from the sample data provided. Both grep suggestions assume not only that the first column is fixed-width, but also that its values will never appear in any way in the second column.

Regards,
Alister
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Searching a string in a particular file name

Hello, I have a file name like FIRST_DPF_DAILY_CUST_0826152322.txt i need to extract the string after the third "_" underscore upto timestamp ends i.e CUST_0826152322 can anyone help me with the code Thank you! Regards Srikanth Sagi (3 Replies)
Discussion started by: srikanth_sagi
3 Replies

2. Shell Programming and Scripting

Searching a string in a file using perl

Hi I would like to read a file using perl and search for a string (last entry). Then read that into an array and do further grep File content for ex: comp=a,value=30,runtime=12,type=lic comp=d,value=15,runtime=2,type=lic comp=a,value=90,runtime=43,type=lic... (1 Reply)
Discussion started by: vivek_damodaran
1 Replies

3. Shell Programming and Scripting

Help in searching a particular string in a file name (not inside the file contents)

Dear Unix Gurus, I am new to shell scripting and in the process of learing. I am trying to find whether a file name has today's date in MMDDYYYY format. I am using the following code and it doesn't seem like working. #!/usr/bin/ksh today=$(date '+%m%d%Y') echo today: $today file=`find... (4 Replies)
Discussion started by: shankar1dada
4 Replies

4. Shell Programming and Scripting

Searching for a specific string in a file

Hi I am trying to search for a certain set of patterns within a file, and then perform other commands based on output. testfile contents: password requisite pam_cracklib.so lcredit=-1 ucredit=-1 ocredit=-1 script: D="dcredit=-1" if then echo $D exists else echo $D doesnt... (8 Replies)
Discussion started by: bludhemn
8 Replies

5. Shell Programming and Scripting

searching each file for a string

Hi Guys... I want to search for each file that contains a particular string. e.g find . -print | xargs grep -i string_name Now my issue is the files that I search in are gzipped. Will I be able to find the string, using the above commands, even if the files are gzipped? Please... (2 Replies)
Discussion started by: Phuti
2 Replies

6. Shell Programming and Scripting

Searching a string in a file

Hi, I am new to unix shell scripting. I have a requirement. Could anyone help me writing the script for the same? Here goes the requirement: I have a config file let's say temp.config. Here is the data in the config file temp.config : ------------- name=victor age=42 state=texas... (5 Replies)
Discussion started by: badrimohanty
5 Replies

7. UNIX for Dummies Questions & Answers

searching for a string in a file

I need to search for a specific string in a file and if this string exist I need to replace it with something else. I am not sure how I could do this, using an if statement. (2 Replies)
Discussion started by: ROOZ
2 Replies

8. Shell Programming and Scripting

Extracting a string from one file and searching the same string in other files

Hi, Need to extract a string from one file and search the same in other files. Ex: I have file1 of hundred lines with no delimiters not even space. I have 3 more files. I should get 1 to 10 characters say substring from each line of file1 and search that string in rest of the files and get... (1 Reply)
Discussion started by: mohancrr
1 Replies

9. Shell Programming and Scripting

Perl: searching for a string in a file...

Hi All, I need to search for a string in a file that I've opened and base a decision on the result. The logic is this: "if the word 'Shared' appears on the first line then do this on the whole file else do this on the whole file " The code I currently have isn't working:... (4 Replies)
Discussion started by: pondlife
4 Replies

10. UNIX for Dummies Questions & Answers

searching for a string though file system

Is there a way to search an entire file system for the occurance of a string..... other than grep. I have a large directory structure and I'm not certain that grep <string> */*/*/*... is all that effective - especially as I can't be sure of the number of levels to go down without heaps of... (3 Replies)
Discussion started by: peter.herlihy
3 Replies
Login or Register to Ask a Question