Editing a file using a script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Editing a file using a script
# 1  
Old 05-28-2003
Editing a file using a script

Hi

I have about 10 config files belonging to software that runs on SCO UNIX. These files contain, amongst many other things, a path which points to the software locations. We normally have to change them manually every time the software is coppied to a new location and when you gotta do a few times a day, it becomes quite laboreous.

I would like to know how to change all these paths using a single script. I'm hoping to have the user enter the 2 different paths once and let the script find the paths in these files (line numbers for the paths vary from file to file) and over write them with the new ones.

It is worth noting that the only alterations that can be made to the config files are the path names. Anything else will cause the software that reads them, to disappear up its own backside.

I hope I'm being clear and I look forward to some help Smilie
# 2  
Old 05-28-2003
When you deploy your software, do you also have a common directory structure associated with it? For instance, if I were to deploy to /home/my-user, would there be common directories under /home/my-user such as sh, log, bin, files? If so, you could develop a common approach to deployment by having your user enter in the common path to the directory.

For example:

#My Deployment Script

echo "Enter Deployment Directory"
read DEPLOY_DIR

if [ ! -d $DEPLOY_DIR ]
then
echo "$DEPLOY_DIR Not A Directory"
exitt 1
fi

export UNIX_ENV=$DEPLOY_DIR

. $UNIX_ENV/envFile #Source in the environment file

#envFile
#Add all path variables to the env file so
#that you never have to change a path #again!

export SH_PATH=$UNIX_ENV/sh
export BIN_PATH=$UNIX_ENV/bin
export LOG_PATH=$UNIX_ENV/log

If this is not an option, then you can used sed to change all of the paths based upon user input. There will be a little effort to ensure your changing the correct line in each file. This can easly be overcome by tagging the line in each file that you want to have changed. Make sure that before you change your original file that you make a backup!
# 3  
Old 05-28-2003
Thanks for your reply google.

Unfortunately I'm only a junior manager here and making changes to the software code itself isn't an option in my position unfortunately...

the second way you propose is more up my street, but I'm not sure how to use the sed command. I've been working with UNIX for 2 days now Smilie

I have written the script that coppies the software and the 10 files in queswtion, but it's a royal pain to edit these files every time. See my problem?
# 4  
Old 05-28-2003
Tools

Edit: replied to wrong thread
# 5  
Old 05-28-2003
Use sed

As far as I understand sed can meet your requirements or you can even do a global replace in vi.
Open the file using vi ,type escape followed by : then type
s/Oldpath/newpath/g .This should change the path through out the file.Also note that if your path contains "/" ,then you will have to escape it using a "\"
For example:

s/old_dir1\/old_dir2\/old_dir3/new_dir1\/new_dir2\/new_dir3/g

alternately you can use sed
sed 's/old_dir1\/old_dir2\/old_dir3/new_dir1\/new_dir2\/new_dir3/g' filename >new_filename
Also if you want to pass the pathnames as argument then you can type a script containing sed which takes the path as arguments
I haven't tried the last solution,but I don't see any major problem with it.

Hope this helps
Saurabh
# 6  
Old 05-28-2003
Wouldn't you have to edit the script with the old and new directories every time? (which kinda defeats the purpose)

There are 152 variations (groups) of these 10 files knocking about in several servers and you choose at random a group of 10 to modify. None of the 152 variations are the same...

Just to throw a spanner in the works and confuse everyone Smilie
# 7  
Old 05-28-2003
Here is the problem as I understand it.
1. You have a ton of scripts that have hard coded paths
2. Each time a new deployment occurs, you must edit the paths
3. Your sick of manually editing the paths!

Proposal:

-- Create a script that anyone can use that will do the following.
1. Copy the files in questions (deploy them were they need to be)
2. For each file and path that needs a change, Prompt the user with the existing directory paths. Ask user if this is correct (if so, exit) if not ask user to specify the path.
3. Validate the users input.
4. Backup the original config files that you are about to change.
5. Use sed to change the path by replacing the old path in the file with what the user just requested.
6. Work on a more robust deployment plan Smilie

Here is a link to some sed info to get you started

Last edited by google; 05-28-2003 at 12:20 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Parsing and Editing a json file with bash script

I am trying to automate editing of a json file using bash script. The file I initially receive is { "appMap": { "URL1": { "name": "a" }, "URL2": { "name": "b" }, "URL3": { "name": "c" }, } WHat I would like to do is replace... (5 Replies)
Discussion started by: Junaid Subhani
5 Replies

2. Shell Programming and Scripting

Editing a file on remote server using shell script locally

Hi Scott, My previous post was not for any school or college projects. I am currently working with a IT company (Cannot provide more details than this). I am trying to implement the below script in my day-to-day work, Apologies for the confusion in previous post :). My question remains same... (4 Replies)
Discussion started by: Nishant Ladiwal
4 Replies

3. Shell Programming and Scripting

Editing a file on remote server using shell script locally

Hi All, I have below requirements for my project: 1. Building a shell script which connects to a remote server 2. running script on local machine as user 'A' 3. connecting to server using user 'B' with password 4. login with a powerbroker role 'P' (asks for same password as 'B') on that... (1 Reply)
Discussion started by: Nishant Ladiwal
1 Replies

4. Shell Programming and Scripting

editing single line in html file in perl script

Hi Folks, It is regarding the perl scripting. I have an html file(many files) which contains the below line in the body tag. <body> <P><STRONG><FONT face="comic sans ms,cursive,sans-serif"><EM>Hello</EM></FONT></STRONG></P> </body> Now I want to read that html file through perl... (3 Replies)
Discussion started by: giridhar276
3 Replies

5. Shell Programming and Scripting

Problems editing file with awk in bash script

Hello dear users, here I have a script to manipulate .csv files that are like this originally: And I need to make a script to delete certain fields. Each field is separated with a comma. So, here is my script (at least a part of it): Field $1 is composed of a name, and then a... (5 Replies)
Discussion started by: sr00t
5 Replies

6. Shell Programming and Scripting

editing a file in a script

Gurus, I need to write a shell script that will calculate hash value of a file, opens the file in an application for example vi editor. The application can read or modify the contents of the file. When application exists second part of my script will kick in and recalculate the hash value. File... (1 Reply)
Discussion started by: c0kazaz
1 Replies

7. Shell Programming and Scripting

editing a file using shell script

HI all, I have file in the below format 1111111111_222222222_3333333 111111_22222_33333 11111111_222222_33333333333 i need to display this file like this 2222222_1111111111 22222_11111111 22222222222_1111111111111 can anyone help me with this Thanks in advance (1 Reply)
Discussion started by: saravanan71184
1 Replies

8. Shell Programming and Scripting

editing excel file through shell script

Hi, I am having a business file in excel having charts based on data already present on it. I would like to add new rows after the existing data and refesh the chart on it using shell script. For example-- In excel file in "sheet1", There is some data in first 10 rows ( from column A to F).... (0 Replies)
Discussion started by: sanjay1979
0 Replies

9. Shell Programming and Scripting

editing a file via a shell script ??

Morning All: I know this might be easy but since I don't do this very often I get stumped real quick... Sun box Solaris 8 ksh... I need to edit a file via a shell script. In this file I need to locate one specific line and then remove that line plus the next 20 line below that.... Any... (2 Replies)
Discussion started by: jimmyc
2 Replies

10. UNIX for Dummies Questions & Answers

Editing a file in a shell script

Using Solaris 8. I need to create a shell script that will edit a text file. I need to look in the text file and do a search and replace. For instance, the text file name is always 'filename'. I need to open filename and replace every instance of 'oldtext' with 'newtext'. 'oldtext' is static. ... (3 Replies)
Discussion started by: jowpup
3 Replies
Login or Register to Ask a Question