replacing variable values in all files in directories


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting replacing variable values in all files in directories
# 1  
Old 12-29-2005
replacing variable values in all files in directories

Hi Guys,

I have some basic unix knowlege but new to shell scripting. I want to replace variable values in all files in sub-directories. Any sugestion will be helpful.

Example.

Main Dir: X
Sub Dir: X1, X2, X3, X4, X5, X6.... X10
Each sub directory has file which contains variable A = 10.

I want to change value to 20 and this value should be replaced in all files in sub directories (X1.. X10)

Thanks
# 2  
Old 12-30-2005
there is always a better way,

Code:
# !/usr/bin/ksh

for file in `find . -name '*' | xargs grep -il 'A = 10'`
do
sed -e 's/A = 10/A = 20/g' $file > temp
mv temp $file
echo "File $file done"
done

exit 0

# 3  
Old 12-30-2005
just a quick point on matrix's script ... the files with the variables being replaced are most likely executable scripts --- you might not want to use "mv temp $file" as that would change the permissions on $file to the default permissions on temp which means most likely 644 ... use "cat temp > $file" instead ...
Code:
#! /usr/bin/ksh

for file in `find . -name '*' | xargs grep -il 'A = 10'`
do
sed -e 's/A = 10/A = 20/g' $file > temp
cat temp > $file
rm temp
echo "File $file done"
done

exit 0

btw, unless you're scripting in csh/tcsh --- there should be no spaces in between the variable name, the equal sign, and the variable value (i.e., A=10 instead of A = 10) ...
# 4  
Old 12-30-2005
All the below 3 lines in the above 2 codes

sed -e 's/A = 10/A = 20/g' $file > temp
cat temp > $file
rm temp

can be replaced by
perl -pi -e 's/A = 10/A = 20/g' $file
## No hassle to create temp file, rename, move, or cat and redirect
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Help in replacing column values

Hello All, I am having the file as below .I need to replace column 9-12 with some other values. In the below file I need to replace 1509 to 1508 and 1508 to 1507 .Can you please help me in how to do that Thanks, Arun ... (10 Replies)
Discussion started by: arunkumar_mca
10 Replies

2. Shell Programming and Scripting

Archiving and moving files into directories, creating directories, etc.

how can i move "dataName".sql.gz into a folder called 'database' and then move "$fileName".tar.gz * .htaccess into a folder called 'www' with the entire gzipped file being "$fileName".tar.gz? Is this doable or overly complex. so mydemo--2015-03-23-1500.tar.gz > database -... (5 Replies)
Discussion started by: wyclef
5 Replies

3. Shell Programming and Scripting

awk file to read values from Db2 table replacing hard coded values

Hi, I want to replace a chain of if-else statement in an old AWK file with values from Db2 table or CSV file. The part of code is below... if (start_new_rec=="true"){ exclude_user="false"; user=toupper($6); match(user, "XXXXX."); if (RSTART ==2 ) { ... (9 Replies)
Discussion started by: asandy1234
9 Replies

4. Shell Programming and Scripting

How to list all the files, directories and sub-directories in the current path except one directory?

Can anyone come up with a unix command that lists all the files, directories and sub-directories in the current directory except a folder called log.? Thank you in advance. (7 Replies)
Discussion started by: Manjunath B
7 Replies

5. Shell Programming and Scripting

Replacing column values

hi all , ( perl) i am trying to replace jst one column in a file for eg month dayofweek dealar car-name jan thurs mercedes c300 feb wed lexus is300 all this data is in a master file and i want to replace jan with 1 feb... (5 Replies)
Discussion started by: technoman
5 Replies

6. Shell Programming and Scripting

Replacing variable is files in same directory

Hi all, I'm writing a script to get values from the user and replace it in another file in the same directory. /usr/bin/sediff 's/$PROJECT/'$PROJECT'/' /ananth/TEMPLATE TEMPLATE is the file I want to replace the variable PROJECT from the script I'm writing but Im not getting it done. Is... (4 Replies)
Discussion started by: Ananthdoss
4 Replies

7. Shell Programming and Scripting

Replacing variable values in html tags

Hi please help me with this . I have a file test.txt with following content $cat test.txt <td>$test</td> <h2>$test2</h2> and I have a ksh with following content $cat test.ksh #!/bin/ksh test=3 test2=4 while read line do echo $line done < test.html I am expecting the output as (4 Replies)
Discussion started by: panduandpavan
4 Replies

8. UNIX for Dummies Questions & Answers

List directories and sub directories recursively excluding files

Hi, Please help me, how to get all the direcotries, its sub directories and its sub directories recursively, need to exclude all the files in the process. I wanted to disply using a unix command all the directories recursively excluding files. I tried 'ls -FR' but that display files as... (3 Replies)
Discussion started by: pointers
3 Replies

9. Shell Programming and Scripting

Replacing File values

Currently I am using the tr command in 3 scenarios for a ksh script. 1) Replacing any spaces in the file with a ~ tr ' ' '~' <$orignalFile> $newFile 2) After certain processing is done at the end of the scirpt i convert the Tilde back to spaces tr ' ' '~' <$newFile> $newFile2 3) Last... (4 Replies)
Discussion started by: hgjdv
4 Replies
Login or Register to Ask a Question