search and replace with data from another file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting search and replace with data from another file
# 1  
Old 03-02-2010
search and replace with data from another file

Can someone help me in solving the problem below.

I have the following two files

Code:
template_file
------------
...other data..
...other data..
FILE_NAME=
...other data..
...other data..

list_file
----------
<file_name1>
<file_name2>
<file_name3>

I need to produce another output file as

Code:
new_template_file
------------------
...other data..
FILE_NAME=/core/local/in/<file_name1> FILE_NAME=/core/local/in/<file_name2> FILE_NAME=/core/local/in/<file_name3>
...other data..
...other data..

Simply, I need to read the list file and prefix the path /core/local/in to the file names and then look for keyword FILE_NAME in the template and replace it with the file names that I received in list file.

Last edited by Scott; 03-02-2010 at 02:09 PM.. Reason: Please use code tags
# 2  
Old 03-02-2010
I am not sure , but are you looking for something like this:
Code:
 
 
awk 'NR==FNR { a="FILE_NAME=/core/local/in/"$1 a ;next } $0 ~ /FILE_NAME/ {print a;next}{print $0}' list_file template_file

# 3  
Old 03-02-2010
Thanks. It produced the following output

Code:
FILE_NAME=/core/local/in/file_name3FILE_NAME=/core/local/in/file_name2FILE_NAME=/core/local/in/file_name1

I am looking for an output of this format
Code:
FILE_NAME=/core/local/in/file_name1 FILE_NAME=/core/local/in/file_name2 FILE_NAME=/core/local/in/file_name3

Is there anyway in your solution you can introduce the space after each file name and also maintain the order that came in list file?

I tried below. But it also produces an unnecessary space in the beginning.
Code:
awk 'NR==FNR { a=" FILE_NAME=/core/local/in/"$1 a ;next } $0 ~ /FILE_NAME/ {print a;next}{print $0}' list_file template_file


Last edited by Scott; 03-02-2010 at 02:10 PM.. Reason: Code tags please...
# 4  
Old 03-02-2010
What about invert the strings being concatenated to get the right order of files,

Code:
a=a "FILE_NAME=/core/local/in/"$1" "

in this case you get the extra space character at the end of the string, if still is relevant for you, try sub for delete the character

Code:
sub(/[ ]*$/, "", a)

before use it
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Nested search in a file and replace the inner search

Hi Team, I am new to unix, please help me in this. I have a file named properties. The content of the file is : ##Mobile props east.url=https://qa.east.corp.com/prop/end west.url=https://qa.west.corp.com/prop/end south.url=https://qa.south.corp.com/prop/end... (2 Replies)
Discussion started by: tolearn
2 Replies

2. Shell Programming and Scripting

Search and replace data

FILENAME: (Orig File) lab computer11node1 { hardware ok 19:50:56:bd:03:c2; MAC-ADDRESS 10.1.1.1; } lab computer11node2 { hardware ok 19:50:56:bd:03:c3; MAC-ADDRESS 10.1.1.2; } FILENAME2: computer11node1 11:50:56:81:33:c5 computer11node2 11:50:56:81:33:c6 computer11node3 11:50:56:81:33:c7... (5 Replies)
Discussion started by: kenshinhimura
5 Replies

3. Shell Programming and Scripting

Replace data of a file with data from another file using shell scripting.

Dears, I'm new to shell scripting and i was wondering if you can help me with following matter. I have a file containing 400,000 records. The file contains two columns like: 00611291,0270404000005453 25262597,1580401000016155 25779812,1700403000001786 00388934,1200408000000880... (1 Reply)
Discussion started by: paniklas
1 Replies

4. Shell Programming and Scripting

awk/sed to search & replace data in first column

Hi All, I need help in manipulating the data in first column in a file. The sample data looks like below, Mon Jul 18 00:32:52 EDT 2011,NULL,UAT Jul 19 2011,NULL,UAT 1] All field in the file are separated by "," 2] File is having weekly data extracted from database 3] For eg.... (8 Replies)
Discussion started by: gr8_usk
8 Replies

5. Shell Programming and Scripting

Using sed to search and replace data

Hi, Kindly need your expertise in this problem. I have to search and replace data. The problem is, the data is in the same format but slightly different content. What I need is sed commands that can work for those "slightly different content". input: ... (3 Replies)
Discussion started by: Alvin123
3 Replies

6. Shell Programming and Scripting

Replace data of one column with data on other file corresponding to transaction ID matched

Hi All, I have two files one of which having some mobile numbers and corresponding value whose sample content as follows: 9058629605,8.0 9122828964,30.0 And in second file complete details of all mobile numbers and sample content as follows and delimeter used is comma(,): ... (8 Replies)
Discussion started by: poweroflinux
8 Replies

7. Shell Programming and Scripting

Find and replace data in text file with data in same file

OK I will do my best to explain what I need help with. I am trying to format an ldif file so I can import it into Oracle oid. I need the file to look like this example. Keep in mind there are 3000 of these in the file. changetype: modify replace: userpassword dn:... (0 Replies)
Discussion started by: timothyha22
0 Replies

8. UNIX for Advanced & Expert Users

search and replace in a file

I have a file (say file1.txt) and I have to search for a line which has a text replace it and replace another string too in the same line. Eg: file1.txt -------- x='hai' y='world' z='unix' x='hai' y='world' x='hai' z='perl' y='world' z="world" k="junk" b="world" z='perl' x='hai'... (3 Replies)
Discussion started by: ammu
3 Replies

9. UNIX for Dummies Questions & Answers

Search and replace in file

Hi guys, I have one file with duplicate string. I want to replace all the occurance of that string with some other string. How can I do that in vi editor? Malay Maru (3 Replies)
Discussion started by: malaymaru
3 Replies

10. Shell Programming and Scripting

search and replace dynamic data in a shell script

Hi, I have a file that looks something like this: ... 0,6,256,87,0,0,0,1187443420 0,6,438,37,0,0,0,1187443380 0,2,0,0,0,10,0,1197140320 0,3,0,0,0,10,0,1197140875 0,2,0,0,0,23,0,1197140332 0,3,0,0,0,23,0,1197140437 0,2,0,0,0,17,0,1197140447 0,3,0,0,0,17,0,1197140543... (8 Replies)
Discussion started by: csejl
8 Replies
Login or Register to Ask a Question