Read from one file-Replace a pattern in another with the current one


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Read from one file-Replace a pattern in another with the current one
# 8  
Old 12-07-2012
Hello,
The awk code is really fascinating and very useful.However it implements a global change
Could it be changed slightly to accomodate the following data structure in the "master file" i.e. the file from which the changes are to be made.
The file could have the structure
Quote:
a=b
i.e. wherever you find string a replace by string b
An example would make this clear:
Quote:
John=Jean
Mary=Marie
This provides explicit replace "rules"and only John would be replaced in the master file and not Johnny.
If the code is modified to meet this requirement, it would be of great use to people like me who work with dictionaries
Many thanks
# 9  
Old 12-07-2012
You mean sth like this..Smilie

Code:
$ cat file1
John=Jean
Mary=Marie

$ cat file2
John rocks the stage
js Mary sfds sdf
mof dfs dfd Johny sdfd

Code:
$ awk 'NR==FNR{A[NR]=$0;next}{for(j in A){split(A[j],P,"=");for(i=1;i<=NF;i++){if($i==P[1]){$i=P[2]}}}}1' file1 file2

Jean rocks the stage
js Marie sfds sdf
mof dfs dfd Johny sdfd

# 10  
Old 12-07-2012
Many thanks. Yes and it works fabulously on a short sample which I tried. I'll test it on a very large sample and get back to you in case there are any glitches.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script to replace a string with pattern read from a file

I have two files blocks.txt and rules.txt. In blocks.txt i have the following entries Linux1 Linux2 Linux3 ..... Linux10 In rules.txt i have the lines where a filename pattern starts like 'blk-name.*' I want to replace 'blk-name' with the names read from blocks.txt file I tried... (2 Replies)
Discussion started by: Jag02
2 Replies

2. Shell Programming and Scripting

Perl script to read string from file#1 and find/replace in file#2

Hello Forum. I have a file called abc.sed with the following commands; s/1/one/g s/2/two/g ... I also have a second file called abc.dat and would like to substitute all occurrences of "1 with one", "2 with two", etc and create a new file called abc_new.dat sed -f abc.sed abc.dat >... (10 Replies)
Discussion started by: pchang
10 Replies

3. Shell Programming and Scripting

Help with ksh-to read ip file & append lines to another file based on pattern match

Hi, I need help with this- input.txt : L B white X Y white A B brown M Y black Read this input file and if 3rd column is "white", then add specific lines to another file insert.txt. If 3rd column is brown, add different set of lines to insert.txt, and so on. For example, the given... (6 Replies)
Discussion started by: prashob123
6 Replies

4. Shell Programming and Scripting

awk - writing matching pattern to a new file and deleting it from the current file

Hello , I have comma delimited file with over 20 fileds that i need to do some validations on. I have to check if certain fields are null and then write the line containing the null field into a new file and then delete the line from the current file. Can someone tell me how i could go... (2 Replies)
Discussion started by: goddevil
2 Replies

5. Shell Programming and Scripting

Finding 4 current files having specific File Name pattern

Hi All, I am trying to find 4 latest files inside one folder having following File Name pattern and store them into 4 different variables and then use for processing in my shell script. File name is fixed length. 1) Each file starts with = ABCJmdmfbsjop letters + 7 Digit Number... (6 Replies)
Discussion started by: lancesunny
6 Replies

6. UNIX for Dummies Questions & Answers

find the file names having specified pattern at specified position in the current directory

I would need a command for finding first 15000 of the file names whose 25th postion is 5 in the current directory alone. I do have this painful command find . -name '5*' | head -15000 | cut -c3- please refine this. Of course the above command also searches in the sub directories... (3 Replies)
Discussion started by: vk39221
3 Replies

7. Shell Programming and Scripting

Read file and for each line replace two variables, add strings and save output in another file

Hi All, I have a file, let's call it "info.tmp" that contains data like this .. ABC123456 PCX333445 BCD789833 I need to read "info.tmp" and for each line add strings in a way that the final output is put /logs/ua/dummy.trigger 'AAA00001.FTP.XXX.BLA03A01.xxxxxx(+1)' where XXX... (5 Replies)
Discussion started by: Andy_ARG
5 Replies

8. Shell Programming and Scripting

Replace a particular pattern in a file

Hi , I have a requirement where i have this file abcd $$xyz=123 $$zef=789 $$mno=123 $$pqr=456 I need to replace $$pqr from 456 to a new value which will be present in a variable to me. I am not aware of sed and awk functionality. Can somebody please show me how to replace this... (6 Replies)
Discussion started by: lifzgud
6 Replies

9. Shell Programming and Scripting

read from a specific pattern from one file and append it to another

Hi! Everyone, Say this file1 -------------- line 1 51610183 420001010 0010CTCTLEDPPOO 2151610183 line 2 2151610183 420001010 0030A2TH2 line 3 2151610183 420001010 0040A2TH3 line 4 2151610183 420001010 ... (0 Replies)
Discussion started by: kinkar_ghosh
0 Replies

10. Shell Programming and Scripting

read file and print additional rows till current year

Hi all i have a file like 2006,1,2 2007,2,3 2008,3,4 I will read this and my output should be like 2006,1,2 2007,1,2 2008,1,2 2007,2,3 2008,2,3 2008,3,4 Giving the explanation, we will read the first line of the file and if the year any other than current year, we will print as many... (1 Reply)
Discussion started by: vasuarjula
1 Replies
Login or Register to Ask a Question