script to match patterns in 2 different files.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting script to match patterns in 2 different files.
# 1  
Old 08-03-2010
script to match patterns in 2 different files.

I am new to shell scripting and need some help. I googled, but couldn't find a similar scenario.

Basically, I need to rename a datafile. This is the scenario -
I have a file, readonly.txt that has 2 columns - file# and name.
I have another file,missing_files.txt that has id and name. Both the files are tab delimited.
This is how the 2 files look -

readonly.txt -
Code:
1|/u01/oradata/smp1.dbf
552|/u15/oradata/smp32.dbf
83|/u12/oradata/gdf12.dbf
24|/u01/oradata/ryt.dbf
52|/u01/oradata/yrr11.dbf

missing_files.txt-
Code:
00552|missing00552.dbf
00083|missing00083.dbf
00001|missing00001.dbf
00024|missing00024.dbf
00052 |missing00052.dbf

Now, this is what I want -
loop through missing_files.txt and if the first column matches the first column in readonly.txt (by ignoring the leading 0s in the second file), then, give me the second column of that line.
For eg, the last line in readonly.txt is 52|/u01/oradata/yrr11.dbf. Now the first column matches the first column of the last line in missing_files.txt (It should not match the first line in missing_files.txt-since 00552 also has 52 in it), then I need to output missing00052.dbf.

This is the script that I have now -
Code:
for i in `cat missing_files.txt`
do
fileid=$(grep $i missing_files.txt | cut -d"|" -f1)
missing_filename=$(grep $i missing_files.txt | cut -d"|" -f2)
filename=$(grep $fileid $smartp1_readonlydbfiles.txt | cut -d"|" -f2)
echo $filename
done

The
Code:
filename=$(grep $fileid $smartp1_readonlydbfiles.txt | cut -d"|" -f2)

doesn't do what I want.

Can someone please help?

Moderator's Comments:
Mod Comment Use code tags please, ty.
# 2  
Old 08-03-2010
Hi

Code:
 awk -F"|" 'NR==FNR{a[$1]=$2;next}{ if(int($1) in a)print a[int($1)];}' readonly.txt missing_files.txt

Guru.
# 3  
Old 08-03-2010
Guru, thanks a lot. I tried it and it looks like I don't need to put this in the for loop. I actually need to get the missing file name (eg. missing00552.dbf) and the corresponding filename (/u15/oradata/smp32.dbf) so that it can be passed into sqlplus to do a -

Code:
alter database rename file 'missing00552.dbf' to '/u15/oradata/smp32.dbf';

# 4  
Old 08-03-2010
Quote:
Originally Posted by mathews
Guru, thanks a lot. I tried it and it looks like I don't need to put this in the for loop. I actually need to get the missing file name (eg. missing00552.dbf) and the corresponding filename (/u15/oradata/smp32.dbf) so that it can be passed into sqlplus to do a -

Code:
alter database rename file 'missing00552.dbf' to '/u15/oradata/smp32.dbf';

Code:
nawk -F'|' 'FNR==NR{f1[$1+0]=$2;next} $1+0 in f1 {print "alter database rename file " q $2 q " to " q f1[$1+0] q ";" }' q="'" readonly.txt missing_files.txt

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Match patterns between two files and extract certain range of strings

Hi, I need help to match patterns from between two different files and extract region of strings. inputfile1.fa >l-WR24-1:1 GCCGGCGTCGCGGTTGCTCGCGCTCTGGGCGCTGGCGGCTGTGGCTCTACCCGGCTCCGG GGCGGAGGGCGACGGCGGGTGGTGAGCGGCCCGGGAGGGGCCGGGCGGTGGGGTCACGTG... (4 Replies)
Discussion started by: bunny_merah19
4 Replies

2. Shell Programming and Scripting

Bash - Find files excluding file patterns and subfolder patterns

Hello. For a given folder, I want to select any files find $PATH1 -f \( -name "*" but omit any files like pattern name ! -iname "*.jpg" ! -iname "*.xsession*" ..... \) and also omit any subfolder like pattern name -type d \( -name "/etc/gconf/gconf.*" -o -name "*cache*" -o -name "*Cache*" -o... (2 Replies)
Discussion started by: jcdole
2 Replies

3. Shell Programming and Scripting

awk to print match or non-match and select fields/patterns for non-matches

In the awk below I am trying to output those lines that Match between file1 and file2, those Missing in file1, and those missing in file2. Using each $1,$2,$4,$5 value as a key to match on, that is if those 4 fields are found in both files the match, but if those 4 fields are not found then missing... (0 Replies)
Discussion started by: cmccabe
0 Replies

4. Shell Programming and Scripting

Match 2 patterns together

How can I quickly print out lines in a datafile which has presence of both patterns in a row of another file. Maybe awk can do it much faster than bash. Patternfile ID1 PAT11 PAT12 ID1 PAT21 PAT22 ID2 PAT31 PAT32 datafile headerline... (2 Replies)
Discussion started by: abh.kumar
2 Replies

5. UNIX for Dummies Questions & Answers

Match patterns from another file and tag

Hi all, I have a file , which has 6 tab delimited fields, with $3 and $4 subfielded with spaces. I wamt to match cols $2,$3,$4 of tmp1 with tmp2, ..and then flag the 5th col if found. tmp1 1756 Xerm XermA XermB XermC XermD AA TT AA GG A 1 1763 Xerm XermA XermB XermC... (3 Replies)
Discussion started by: senhia83
3 Replies

6. Shell Programming and Scripting

Match 2 different patterns and print the lines

Hi, i have been trying to extract multiple lines based on two different patterns as below:- file1 @jkm|kdo|aas012|192.2.3.1 blablbalablablkabblablabla sjfdsakfjladfjefhaghfagfkafagkjsghfalhfk fhajkhfadjkhfalhflaffajkgfajkghfajkhgfkf jahfjkhflkhalfdhfwearhahfl @jkm|sdf|wud08q|168.2.1.3... (8 Replies)
Discussion started by: redse171
8 Replies

7. Shell Programming and Scripting

Using AWK to match CSV files with duplicate patterns

Dear awk users, I am trying to use awk to match records across two moderately large CSV files. File1 is a pattern file with 173,200 lines, many of which are repeated. The order in which these lines are displayed is important, and I would like to preserve it. File2 is a data file with 456,000... (3 Replies)
Discussion started by: isuewing
3 Replies

8. Shell Programming and Scripting

Find files that do not match specific patterns

Hi all, I have been searching online to find the answer for getting a list of files that do not match certain criteria but have been unsuccessful. I have a directory that has many jpg files. What I need to do is get a list of the files that do not match both of the following patterns (I have... (21 Replies)
Discussion started by: nikos-koutax
21 Replies

9. UNIX for Dummies Questions & Answers

script to search patterns inside list of files

>testfile while read x do if then echo $x >> testfile else fi if then echo $x >> testfile else fi done < list_of_files is there any efficient way to search abc.dml and xyz.dml ? (2 Replies)
Discussion started by: dr46014
2 Replies

10. Shell Programming and Scripting

Script for searching a pattern in 5 files and deleting the patterns given

Hi All, I have written the below script that searches for the pattern in a file and delete them if present. please can some one have a look and suggest the changes in the script. #!bin/sh # The pattern that user want to add to the files echo "Enter the pattern of the redirect" read... (4 Replies)
Discussion started by: Shazin
4 Replies
Login or Register to Ask a Question