Find and replace folder of files with $var


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find and replace folder of files with $var
# 1  
Old 07-16-2012
Find and replace folder of files with $var

I want to scan through all the files in the folder and replace all instances of $file_X within the file with the variable $X defined in my bash script on my debian 6.0 install.

For example, if the file contains $file_dep I want it to be replaced with the value of the variable $dep defined in my bash script.

Here is an example of my config file:
Code:
IP address = $file_dep

Here is an xample of a definition in my bash script
Code:
$dep=192.168.0.1

Here is the best starting point I could find using google.
Code:
# Replace Config Values
cd /home/config
find . -type f -print0 | xargs -0 sed -i "s/$szAnswer1/$szAnswer2/g"

My pseudo code (im not good with bash)
Code:
Find $FILE_X
Replace $FILE_X with value $X

Can anyone offer me a bit more help?

Thanks,

James
# 2  
Old 07-16-2012
This seems a clunky way to go about things, have you considered instead of doing this, simply adding a header/resource file that all of your scripts can source?

Code:
$ config.rc
IP_ADDY="10.0.0.10"

and then in any/all of the files:
Code:
. /path/to/config.rc

Which will source the profile, and then you could simply replace static defines with the variable name in the file you're sourcing (So that in the future you just have to change it in the one file):
Code:
echo "\$dep=192.168.0.1" | sed 's/\(^\$dep=\)\(.*$\)/\1\$IP_ADDY/g'

Here's how I'd do the find statement, with a loop:
Code:
for file in $(find /home/config -type f -name -exec ls {} \;); do 
if [ "$(grep -c '^\$dep=' $file)" -gt 0 ]; then
sed 's/\(^\$dep=\)\(.*$\)/\1\$IP_ADDY/g' $file > /tmp/file.out
cp /tmp/file.out $file
fi
done

If you need to or want to add in a line for sourceing a resource file, you could add these before the cp line above (Can do this other ways, this is just an easy way I know):
Code:
ed -s  /tmp/file.out << EOF
2i
. /path/to/configrc
.
w
q
EOF

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to replace the first and last character which is pipe symbol in all files within a folder?

with in my files i have the data like this, starting with a pipe and ending the line with a pipe. all i want is to replace the first and last pipe , remove those trying to use following sed command, but it is only showing on the screen the entire data of the file as if it removed, but when i... (4 Replies)
Discussion started by: cplusplus1
4 Replies

2. Shell Programming and Scripting

Find common lines between all of the files in one folder

Could it be possible to find common lines between all of the files in one folder? Just like comm -12 . So all of the files two at a time. I would like all of the outcomes to be written to a different files, and the file names could be simply numbers - 1 , 2 , 3 etc. All of the file names contain... (19 Replies)
Discussion started by: Eve
19 Replies

3. Shell Programming and Scripting

Replace character in files of entire folder? sed? or what?

Hello, I do have several files in one folder each file contains measurement data. for each file I would like to replace the character "," by "." ? How can I do this and how can I do this for each file at once? E.G. data_1.dat, data_x.dat (original version) data_1out.dat, data_x_out.dat... (10 Replies)
Discussion started by: rollinator
10 Replies

4. UNIX for Dummies Questions & Answers

find files on first level folder

Hi: I have: folderA |----folderB |----folder1 |----folder2 |----folder3 |----folder3.1 Question: How can I find *.txt ONLY from /folderA/folderB/ and not the others folder1,2,3?? I tried: find... (8 Replies)
Discussion started by: iga3725
8 Replies

5. Shell Programming and Scripting

Find all text files in folder and then copy to a new folder

Hi all, *I use Uwin and Cygwin emulator. I´m trying to search for all text files in the current folder (C/Files) and its sub folders using find -depth -name "*.txt" The above command worked for me, but now I would like to copy all found text files to a new folder (C/Files/Text) with ... (4 Replies)
Discussion started by: cgkmal
4 Replies

6. UNIX for Dummies Questions & Answers

To find files with specific dates and cp another folder.

Hi all, We have an existing script: find /u03/oraprod/perpcomn/admin/out -type f -ctime +7 \ -exec cp {} "/u08/oraprod/backup/cout" \; Which is to find all files more than 7 days and copy to another folder. However I would like to only list files with Sep 29, and cp to another folder. ... (2 Replies)
Discussion started by: *Jess*
2 Replies

7. Shell Programming and Scripting

find how many files are in use in a folder

How can i find how many files are in use in a folder or its sub folders in unix i tried with fuser .. but i coulnt getting ... (3 Replies)
Discussion started by: smongam
3 Replies

8. Shell Programming and Scripting

How to find files in current folder only?

How do I find files in current folder only? We are on AIX 5.3, so maxdepth is not supported. I tried to do this find /dir1/dir2/dir3/dir4 -prune -type f to display all files in /dir1/dir2/dir3/dir4 only but it does not show any files. Somehow the -prune option works for dir3 level... (7 Replies)
Discussion started by: Hangman2
7 Replies

9. Shell Programming and Scripting

How To replace Control-M in all files in a folder

hi all, I copied set of files from a linux machine to an aix machine but in binary mode copy , ASCII mode copy both leed to control M charecters in most of the files. Any shell script/C script to remove control M charecters in all files in a given directory. Pls reply if you are aware... (10 Replies)
Discussion started by: padpa
10 Replies

10. Shell Programming and Scripting

Replace string in all files in a folder and subfolders.

i need to change string in all files in current folder and all subfolders. i wrote the following script. It works good except it dont delete temp file from subfolders. for z in `find . -type f -name "*.html" -o -name "*.htm"`; do sed -e 's@abc@xyz@g' $z>temp; mv temp $z; done any idea?... (1 Reply)
Discussion started by: crazynups
1 Replies
Login or Register to Ask a Question