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
# 8  
Old 10-29-2011
Hi,

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

Code:
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[i])}1' rich-input.txt "$line" > temp
  echo mv temp "$line"
done
bash-3.2#

Ran code

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??
# 9  
Old 10-29-2011
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
# 10  
Old 10-29-2011
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

Code:
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
# 11  
Old 10-29-2011
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...
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
  mv temp "$line"
done

--ahamed
This User Gave Thanks to ahamed101 For This Post:
# 12  
Old 10-29-2011
Remove the echo in front of the "mv" command.

echo mv temp "$line" should read as mv temp "$line"
This User Gave Thanks to dude2cool For This Post:
# 13  
Old 10-29-2011
Hi Dude2Cool and rdcwayx

Looks like Its good !!

The final code looks like
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"
mv temp "$line"
done

Just one small thing please.

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

For example:

Code:
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 !

Last edited by richsark; 10-29-2011 at 12:47 AM..
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