find and replace in a directory using an input file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting find and replace in a directory using an input file
# 1  
Old 10-28-2011
how to find and replace in a directory using an input file

Hi folks,

I need help to finish this script please.

see below:

I have an input file with all the IP address to names formated like so in a txt file

addnsr1pri 166.7.3.105
addnsr1sec 166.2.100.22
addnsr2pri 166.2.220.121
addnsr2sec 166.3.68.45
addnsr3pri 166.3.68.67
addnsr3sec 166.5.4.211
addnsr4pri 166.2.100.32
addnsr4sec 166.6.4.100

I like to feed this input text file and do a search and replace in a directory c:\dhcp
In that directory there are subfolders as well.

In addition, there are comment lines with #, I dont need to touch those, leave as is please.

Can someone help with the creation of this modified script. I also dont need a .bak created.

So the end result should look like

From:
option domain-name-servers addnsr4pri.fs.us., addnsr4sec.fs.us.;

to:
option domain-name-servers 166.2.100.32,166.6.4.100;

Yes I am a newbie.

Code:
#!/usr/bin/perl -w
use strict;

# Open input file in read mode
open INPUTFILE, "rich-input.txt", $ARGV[0] or die $!;

# Read the input file line by line :
while (my $input_line = <INPUTFILE>) {
$modif = "s/option domain-name-servers / bar /g";
if (preg_match('/option domain-name-servers/i', ) {(?=.*[a-z])(?=.*[A-Z])
preg_replace

Need help to finish it up please

Thanks

Last edited by richsark; 10-28-2011 at 11:00 PM.. Reason: Message update
# 2  
Old 10-28-2011
If the command is fine, remove echo, and run again.
Code:
find /dhcp -type f ! -name "*.bak" |while read line
do
  awk 'NR==FNR{a[$1]=$2;next} !/^#/ {for (i in a) if ($0~i) sub(i ".fs.us.",a[i])}1' rich-input.txt "$line" > temp
  echo mv temp "$line"
done

# 3  
Old 10-28-2011
I'm afraid the OP is on Windows and don't have shell, find and awk.

---

You can install cygwin or ask about a pure perl solution (but I think something like PerlMonks is a better place).
# 4  
Old 10-28-2011
Hi rdcwayx,

Thanks kindly for that shell script. However, I don't seem to get any results

Code:
bash-3.2# ./test.sh 
mv temp dhcp/dhcp.in

Code:
drwxr-xr-x   6 richsark staff  204 Oct 28 22:26 .
drwxr-xr-x+ 22 richsark  staff  748 Oct 28 22:23 ..
drwxr-xr-x   3 root         staff  102 Oct 28 22:22 dhcp
-rw-r--r--   1 richsark staff  188 Oct 28 22:18 rich-input.txt
-rw-r--r--   1 root         staff    0 Oct 28 22:26 temp
-rwxrwxrwx   1 richsark  staff  180 Oct 28 22:22 test.sh

your code:
Code:
find dhcp -type f ! -name "*.bak" |while read line
do
  awk 'NR==FNR{a[$1]=$2;next} {for (i in a) if ($0~i) sub(i,a[i])}' rich-input.txt "$line" > temp
  echo mv temp "$line"
done

my input file

Code:
addnsr1pri 166.7.3.105
addnsr1sec 166.2.100.22
addnsr2pri 166.2.220.121
addnsr2sec 166.3.68.45
addnsr3pri 166.3.68.67
addnsr3sec 166.5.4.211
addnsr4pri 166.2.100.32
addnsr4sec 166.6.4.100

Test file to use the script located in /Users/richsark/test/dhcp/dhcp.in

Code:
option domain-name-servers addnsr4pri.fs.us., addnsr4sec.fs.us.;

I also then removed the echo line, tried again that zeroed out my dhcp.in file

Thanks for your continued help

Last edited by richsark; 10-28-2011 at 11:37 PM.. Reason: claification
# 5  
Old 10-28-2011
You need update the patch for file rich-input.txt, then run the script by bash -x test.sh , paste the output here.
Code:
find /Users/richsark/test/dhcp -type f ! -name "*.bak" |while read line
do
  awk 'NR==FNR{a[$1]=$2;next} {for (i in a) if ($0~i) sub(i,a[i])}' rich-input.txt "$line" > temp
  echo mv temp "$line"
done

cat temp

# 6  
Old 10-29-2011
My Bad, missed type the dir still no go

I see a temp but not a directory nor doing a less shows nothing 0 bytes

Code:
bash-3.2# bash -x test2.sh 
+ read line
+ find /Users/richsark/test-US/dhcp -type f '!' -name '*.bak'
+ awk 'NR==FNR{a[$1]=$2;next} {for (i in a) if ($0~i) sub(i,a[i])}' rich-input.txt /Users/richsark/test-US/dhcp/dhcp.in
+ echo mv temp /Users/richsark/test-US/dhcp/dhcp.in
mv temp /Users/richsark/test-US/dhcp/dhcp.in
+ read line

# 7  
Old 10-29-2011
Just realize, Looks you don't use my code. the number 1 is missed, it is used to print the result to temp file

Code:
find /Users/richsark/test-US/dhcp -type f ! -name "*.bak" |while read line
do
  awk 'NR==FNR{a[$1]=$2;next} !/^#/ {for (i in a) if ($0~i) sub(i ".fs.us.",a[i])}1' rich-input.txt "$line" > temp
  echo mv temp "$line"
done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Replace character string in txt file using input file(S)

Hi I have a large txt file on my AIX server and I need to replace some text using two other files. So filename1 has about 500 lines similar to: txtcode SYStem100 I have the string I want to change in string2 and the new stringname in string3. Does anyone know a way of doing this? I have... (1 Reply)
Discussion started by: Grueben
1 Replies

2. Shell Programming and Scripting

Avoid overwriting backup file when multiple entries need to replace in one file input from another

Hello, I have been working on script in which search and replace the multiple pattern. 1. update_params.sh read the multiple pattern from input file ParamMapping.txt(old_entry|New_entry) and passing this values one by one to change_text.sh 2. change_text.sh read... (0 Replies)
Discussion started by: ketanraut
0 Replies

3. Shell Programming and Scripting

Shell script to find and replace contents of files in directory

Hi all This is my first post. Please bear with me with all my mistakes. I started learning shell since couple of days now and this might be quite basic for all, i want to search for files in a directory containing specific string and replace it with new string. The code i wrote is quite bulky... (2 Replies)
Discussion started by: theprogrammer
2 Replies

4. Shell Programming and Scripting

Find ordinary files in directory input by user

I need to make a shell script that accepts a directory input by the user. The program searches for the directory and finds if it exists or not. Then if it does exist, it outputs the number of files within that directory. Here's what I have so far. result= echo "Please input a directory:... (5 Replies)
Discussion started by: itech4814
5 Replies

5. Shell Programming and Scripting

how to find files and replace them in a directory in Shell scripting

I have a directory /java/unix/data In data directory i have so many files from which i want to find some files who look alike below.(there are number of such below such files as well different files too in the data directory) -68395#svg.xml -56789#ghi.xml -67894#gjk.org -56734#gil.txt I... (6 Replies)
Discussion started by: pratima.kumari
6 Replies

6. Homework & Coursework Questions

[Scripting]Find & replace using user input then replacing text after

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: (o) Checkout an auto part: should prompt the user for the name of the auto part and borrower's name: Name:... (2 Replies)
Discussion started by: SlapnutsGT
2 Replies

7. Shell Programming and Scripting

find & replace with user input

Trying to create a script/executable to replace "abc" text string in "myfile.htm" with input from a pop-up field. For example, launch this thing and a prompt is popped up asking the user to input what "abc" should be replaced with, then it inserts what the user inputs in place of abc in the... (3 Replies)
Discussion started by: mike909
3 Replies

8. UNIX for Dummies Questions & Answers

how to use sed or perl command to find and replace a directory in a file

how to use sed command to find and replace a directory i have a file.. which contains lot of paths ... for eg.. file contains.. /usr/kk/rr/12345/1 /usr/kk/rr/12345/2 /usr/kk/rr/12345/3 /usr/kk/rr/12345/4 /usr/kk/rr/12345/5 /usr/kk/rr/12345/6 /usr/kk/rr/12345/7... (1 Reply)
Discussion started by: wip_vasikaran
1 Replies

9. Shell Programming and Scripting

find and replace string in a directory files

Hi, I have a directory has DIR1 and the D1 directory has 200+ files. I want change the string from "Bangalore" to "Bangaluru" in all files in the D1 directory. Thanks (2 Replies)
Discussion started by: koti_rama
2 Replies

10. Shell Programming and Scripting

Replace text in input file

I wish to replace values of specific parameters in an input file for batch runs of a java code. It's essentially a nested for-loop sorta like this: valuearray1 contains values for param1 valuearray2 contains values for param2 for (all values in valuearray1) go into specific position in... (2 Replies)
Discussion started by: daphantomica
2 Replies
Login or Register to Ask a Question