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.

#!/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

If the command is fine, remove echo, and run again.

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)}1' rich-input.txt "$line" > temp
  echo mv temp "$line"
done

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).

Hi rdcwayx,

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

bash-3.2# ./test.sh 
mv temp dhcp/dhcp.in
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:

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)}' rich-input.txt "$line" > temp
  echo mv temp "$line"
done

my input 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

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

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

You need update the patch for file rich-input.txt, then run the script by bash -x test.sh , paste the output here.

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)}' rich-input.txt "$line" > temp
  echo mv temp "$line"
done

cat temp

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

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)}' 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

Just realize, Looks you don't use my code. the number 1 is missed, it is used to print the result to temp file

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)}1' rich-input.txt "$line" > temp
  echo mv temp "$line"
done

Hi,

I have been using your code. maybe I don't follow sorry

bash-3.2# cat test2.sh 
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)}1' rich-input.txt "$line" > temp
  echo mv temp "$line"
done
bash-3.2# 

Ran code

bash-3.2# ./test2.sh 
mv temp /Users/richsark/test-US/dhcp/dhcp.in
bash-3.2# ls -al
total 32
drwxr-xr-x   7 richsark  staff  238 Oct 28 23:13 .
drwxr-xr-x+ 22 richsark  staff  748 Oct 28 23:11 ..
drwxr-xr-x   3 root         staff  102 Oct 28 22:48 dhcp
-rw-r--r--   1 richsark  staff  188 Oct 28 22:18 rich-input.txt
-rw-r--r--   1 root         staff   53 Oct 28 23:13 temp
-rwxrwxrwx   1 richsark  staff  170 Oct 28 22:58 test.sh
-rwxrwxrwx   1 root         staff  224 Oct 28 23:11 test2.sh

Did i miss something??

Can you please check if the file "temp" is modified as per your requirement?
If so, remove "echo" from the code and re-run!

--ahamed

Just ran your last code, it runs, I see a temp but its not a directory and I cat it, its nothing there.

You think we are close?

---------- Post updated at 11:20 PM ---------- Previous update was at 11:19 PM ----------

ok ... re-running... SB

---------- Post updated at 11:24 PM ---------- Previous update was at 11:20 PM ----------

ok !! better,

I removed the temp

I removed the echo line

Ran it.. I see a file named temp

cat the file and I see the correct contents

option domain-name-servers 166.2.100.32,166.6.4.100;

Shouldn't it have the file name, since I will have many files to change I need them to maintain there name somehow.

Will standby.

Thanks kindly

The actual files are modified and are present at the respective path!
Check this file on your system "/Users/richsark/test-US/dhcp/dhcp.in"

Don't remove the entire echo line... Use this...

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)}1' rich-input.txt "$line" > temp
  mv temp "$line"
done

--ahamed

1 Like

Remove the echo in front of the "mv" command.

echo mv temp "$line" should read as mv temp "$line"

1 Like

Hi Dude2Cool and rdcwayx

Looks like Its good !!

The final code looks like

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)}1' rich-input.txt "$line" > temp
#  echo mv temp "$line"
mv temp "$line"
done

Just one small thing please.

if my file has a space ( in case it does )

For example:

option domain-name-servers addnsr1pri.us.fs, addnsr1sec2.us.fs;

When it gets re-done, I just need the space closed.

option domain-name-servers addnsr1pri.us.fs, addnsr1sec2.us.fs

option domain-name-servers 166.2.100.32,166.6.4.100;

Thanks !