2 files replace multiple occurances based on a match


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting 2 files replace multiple occurances based on a match
# 1  
Old 05-06-2018
2 files replace multiple occurances based on a match

Hi All,

I need some help trying to achieve the below but everything I've tried has failed, I have 2 files which i'm trying to carry out a match based on the first column from file 1, take that value find it in file 2 if found replace it with the second column from File 1

Lookup File: File 1
Code:
4110,RNXXX
2730,RBSAF
1500,RHYDI

File that requires replace function : File 2
Code:
1,4110,4110,,,4110,4110,4110,4110,4110,4110,
2,4111,4111,,,4111,4111,4111,4111,4111,4111,
3,,,,,,,,,,,
4,,,,,,,,,,,
5,4115,4115,,4115,4115,4115,4115,4115,4115,4115,
6,4116,4116,,4116,4116,4116,4116,4116,4116,4116,
7,4117,4117,,,4117,4117,4117,4117,4117,4117,
8,,,,,,,,,,,
9,4125,4125,,,4125,4125,4125,4125,4125,4125,
10,4126,4126,,,4126,4126,4126,4126,4126,4126,
11,4127,4127,,,4127,4127,4127,4127,4127,4127,
12,,,,,,,,,,,
13,4173,4173,,4173,4173,4173,4173,4173 2730 2734,4173,4173,
14,4174,4174,,4174,4174,4174,4174 2733,4174 2730,4174,4174,
15,4175,4175,,4175,4175,4175,4175,4175,4175,4175,

Any help you can offer would be much appreaciated.
# 2  
Old 05-06-2018
Now, this is a problem that has been solved umpteen times in these fora, so the solution could be called a "standard method" or so.
Please show anything that you tried on your own, or even search results that could fit or were a "near miss".
# 3  
Old 05-06-2018
RudiC, This is one of the methods i was trying but this didnt work. Any help or direction on where this has been solved previously would be appreacited.

Thanks

Code:
for i in $(cat file1.txt | cut -d, -f1)
do
replace=`grep $i file1.txt | cut -d, -f2`
cat file2.txt | sed 's/'$1'/'$replace'/g' >> file2_a.txt
done

# 4  
Old 05-06-2018
I think you want $i not $1 in the sed expression.
Then, it is unprecise: a grep is searched in the whole line, can match on the wrong side. Can be improved by boundaries e.g. grep "^$i," file1.txt.
Last but not least, the files are read very often. It makes sense to look for more efficient solutions!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Data match 2 files based on first 2 columns matching only and join if match

Hi, i have 2 files , the data i need to match is in masterfile and i need to pull out column 3 from master if column 1 and 2 match and output entire row to new file I have tried with join and awk and i keep getting blank outputs or same file is there an easier way than what i am... (4 Replies)
Discussion started by: axis88
4 Replies

2. Shell Programming and Scripting

Replace multiple positions in records which match crireria

I have a test file a.txt 001 123 456 789 002 This is just a 001 test data 003 file. I want to clear columns 5 and 6 if the first 3 characters are 001 using awk. I tried following but does not work. Any suggestions? awk 'BEGIN{OFS=FS=""} {if (substr($0,1,3)=="123") $5=" "; $6="... (20 Replies)
Discussion started by: Soham
20 Replies

3. Shell Programming and Scripting

New files based off match or no match

Trying to match $2 in original_targets with $2 of new_targets . If the two numbers match exactly then a match.txt file is outputted using the information in the new_targets in the beginning 4 fields $1, $2, $3, $4 and value of $4 in the original_targets . If there is "No Match" then a no... (2 Replies)
Discussion started by: cmccabe
2 Replies

4. Shell Programming and Scripting

Count the number of occurances for multiple files

I have some text files as shown below. I would like to add the values of each string. Your help would be appreciated!! file1.txt SUS 2 PRS 2 ALI 1 PRS 1 GLK 2 file2.txt PRS 3 GLK 6 SUS 18 Desired output SUS 20 PRS 6 (2 Replies)
Discussion started by: arch
2 Replies

5. Shell Programming and Scripting

Help with replace line based on specific pattern match

Input file data20714 7327 7366 detail data20714 7327 7366 main data250821 56532 57634 detail data250821 57527 57634 main data250821 57359 57474 main data250821 57212 57301 main data250821 57140 57159 detail data250821 56834 57082 main data250821 56708 56779 main ... (3 Replies)
Discussion started by: perl_beginner
3 Replies

6. Shell Programming and Scripting

Counts based on occurances

Hi, I have a file with 2500 entries. There are many duplicates,triplicates symbols in my file in the first column and the second column has categories(high/medium/low) . I want to have count for the occurances of each category for each unique symbol ABC high ABC high ABC medium ABC ... (2 Replies)
Discussion started by: Diya123
2 Replies

7. Shell Programming and Scripting

Replace multiple occurances of same character with a single character.

Hi all, Greetings, I have the following scenario, The contents of main file are like : Unix|||||forum|||||||||||||||is||||||the||best so||||||be|||||on||||||||||||||||||||||||||||||||||||||||||||it And i need the output in the following form: Unix=forum=is=the=best so=be=on=it ... (3 Replies)
Discussion started by: dipanchandra
3 Replies

8. Shell Programming and Scripting

match all occurances of session id with one regex?

So far I have this little regex to match sessionids: session.id={32}What must I add to make it match all occurances i want to match?session_id=993e3cf23ffff68a2b619518829192b9 ?session_id=993e3cf23ffff68a2b619518829192b9 &session_id=993e3cf23ffff68a2b619518829192b9... (1 Reply)
Discussion started by: lowmaster
1 Replies

9. Shell Programming and Scripting

Replace 2 Occurances of Space (sed)

I have a large file that looks like the below output: system SUNWxwmod X Window System kernel modules system SUNWxwoft X Window System optional fonts system SUNWxwopt X Window System Optional Clients system ... (1 Reply)
Discussion started by: hxman
1 Replies

10. Shell Programming and Scripting

Match and replace in different files

Hi, I want to replace the content in 3rd column in the first file by the line in the 2nd file (a list) if matches with 1st column of 2nd file. There are many 1st files in different directories: File1 172.27.1.15 222 I_J_Mar_Sci_34_27.pdf 172.27.28.1 489 Sci_Cult_71_60.pdf... (3 Replies)
Discussion started by: srsahu75
3 Replies
Login or Register to Ask a Question