Making a script for editing


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Making a script for editing
# 1  
Old 12-18-2009
Making a script for editing

Hello all. I am trying to make a script to edit any file by replacing a string with a string. The script should be able to be applied to any given file. for instance it should be able to replace "foo" with "bar" in the file myFile.txt. I know i need to make a back up file for this to work. I just dont know how to make the sed command for a file that has to be inputted. so far i have

#!/bin/sh
sed s/$1/$2/g ---this is where the input file name should be right??---

My script is called subst1. So i should be able to give the command

./subst1 $1 $2 -file name??-

any words of wisdon would be greatly appreciated.
# 2  
Old 12-18-2009
Code:
#!/bin/bash
old=$1
new=$2
filename=$3
gawk -v o="$old" -v n="$new" '{ gsub(o,n) ;print}' $filename > newfile

# 3  
Old 12-19-2009
Code:
#!/bin/sh
sed "s/$1/$2/g" "$3"

You can then call the script like this:
Code:
./subst1 foo bar file

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Help me making this script

This script is executed whenever a new vehicle is added to the cycle-motor park of campus. The script asks for the following information about the car and adds a new line to the vehicle file.txt: name (name of an animal, unique identifier), color, mark, model, type (e.g., electrical, manual),... (2 Replies)
Discussion started by: andre2222
2 Replies

2. Shell Programming and Scripting

Need Help With making this script

Hello, im a new user on this site and learning scripting very slowly at a understanding pace. However i am up with a challenge and require help completing this. The script has to include arguments, variables, decisions and loops. So the script is about calculating the broadcast address for any... (5 Replies)
Discussion started by: tHe666
5 Replies

3. Shell Programming and Scripting

[Solved] Need help in editing a script

Hi, I have one script in my cronjob, which is fetching file from a ftp site and making a copy with today's date and time. This is a new setup. There was a instance when test_bill.txt was not present on ftp.xxxx_xxxx.com and when this job ran and did not fetched file, still it send mail of... (5 Replies)
Discussion started by: solaris_1977
5 Replies

4. Shell Programming and Scripting

I could use some help with making a script

I run a small instrument lab. We track our user's time on the instruments with a very manual process of 'last wtmp.1' then cut/paste data into spreadsheets. My boss makes the initial spreadsheets then I convert and format them for uploading into our billing software (COReS). Cores is looking for a... (8 Replies)
Discussion started by: jpontius
8 Replies

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

6. Solaris

editing files with script

hi guys, We have to implement new local (/etc/default/login) USER security policy on almost 50 stations. so editing /etc/default/login and /etc/default/passwd will be way too long work. Can we do the same using some script, I mean editing the above files and putting variables as RETRIES=3, ... (5 Replies)
Discussion started by: Asteroid
5 Replies

7. Shell Programming and Scripting

SH Script help. editing string

I have a string that looks like this username|field1|field2|field3 the data has a delimiter of "|" how can i edit field1, keeping the rest of the data the same also how can i edit field2 and 3. (3 Replies)
Discussion started by: nookie
3 Replies

8. Shell Programming and Scripting

making script

hello experts cany any one help me i want to make one script which can rlogin to another machine . but it should not ask me username/password from me of another machine it should take the username and password from the script only. please help me out. regards, shary (2 Replies)
Discussion started by: shary
2 Replies

9. Shell Programming and Scripting

Need help making a script

Here is what I have: #!/bin/bash # Setup year date and month YR=`date +%Y '{print $6}'` MON=`date +%b '{print $2}'` DAY=`date +%d '{print $3}'` file=$YR$MOY$DOM # clear # Dump database using USER/PASS to ..sql mysqldump --user=me -ppass database > database-db.$file.sql The YR, MON and... (2 Replies)
Discussion started by: npereira
2 Replies

10. Shell Programming and Scripting

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... (45 Replies)
Discussion started by: Ypnos
45 Replies
Login or Register to Ask a Question