How to replace specific contents in a file?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to replace specific contents in a file?
# 1  
Old 02-14-2011
How to replace specific contents in a file?

From the existing file, I need to replace specific contents possibly with var every time when the user changes the var.
e.g the contents in the file file.txt is 'My name is $n and I am $y years old' and every time user changed the var outside the file, the contents of the file should be created with the specific words updated.
# 2  
Old 02-14-2011
Sounds like something sed could do.
Please show sample input file, and your desired outcome.
# 3  
Old 02-14-2011
It's basically a file called db.txt:
Code:
dbname: database1
dbusername: samplename1
dbpwd: pwd1

The actual file is more complicated than this, so if someone else input database2, samplename2 and pwd2 from stdin, the db.txt file should change its contents to
Code:
dbname: database2
dbusername: samplename2
dbpwd: pwd2


Last edited by Yogesh Sawant; 02-15-2011 at 07:27 AM.. Reason: added code tags
# 4  
Old 02-14-2011
Code:
$ cat db.sh

echo "dbname: $1"
echo "dbusername: $2"
echo "dbpwd: $3"

$ db.sh database2 samplename2 pwd2

# 5  
Old 02-14-2011
Here is what I do for things like this firstly setup your template file with tags surrounded by "%%" eg:
Code:
dbname: %%DBNAME%%
dbusername:  %%DB_USER_NAME%%
dbpwd:  %%DB_PWD%%

Now set and export environment variables with the values you want and then call expand.sh:
Code:
DBNAME="testing1"; export DBNAME
DB_USER_NAME="Test user"; export DB_USER_NAME
DB_PWD="/home/testing1"; export DB_PWD
expand.sh mytemplate > template.expanded

expand.sh looks like this:
Code:
awk -F% ' /%%/ {
 while(match($0, "%%[A-Za-z0-9_]+%%")) {
    tmp = substr($0, 1, RSTART-1)
    var = substr($0, RSTART+2, RLENGTH-4);
    printf "%s%s", tmp, ENVIRON[var];
    $0=substr($0,RSTART+RLENGTH);
 }
 print; next } 1' $@


Last edited by Chubler_XL; 02-14-2011 at 07:52 PM.. Reason: Some format fixes
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to place specific contents filename within text file

I am trying to use awk to place the contens of a filename in $1 and $2 followed by the data in the text file. Basically, put the filename within the text file. There are over 1000 files in the directory and as of now each file is saved with a unique name but it is not within the file. Thank you... (10 Replies)
Discussion started by: cmccabe
10 Replies

2. Shell Programming and Scripting

Replace contents of a file

Hello, I need help to replace a value by a new one. I've got a script, that will get directory size in ko, then write the size value return in a log file : The patch is given in crontab, and use with $1 in the script. I am looking for help to replace, after: the old value without to... (6 Replies)
Discussion started by: Aswex
6 Replies

3. UNIX for Dummies Questions & Answers

How To Replace Contents in a File?

How can i replace the contents in a particular line of a file. <FOLDERMAP SOURCEFOLDERNAME="FFCB-2012" SOURCEREPOSITORYNAME="Repo_DEV" TARGETFOLDERNAME="TEST" TARGETREPOSITORYNAME="Dev_Repo"/> For Example I want to replace the SOURCEREPOSITORYNAME="Repo_DEV" to... (3 Replies)
Discussion started by: Ariean
3 Replies

4. Shell Programming and Scripting

Replace Contents from One file into another

Hi Friends, I have two input files cat input1 chr1 100 200 chr1 200 300 chr2 300 400 cat input2 chr1 hello monday 10 20 . - . sometext chr1 hello monday 20 30 . - . sometext chr2 hello monday 30 40 . - . sometext Now, I want to replace $1, $4 and $5 of input2 with $1, $2 and... (1 Reply)
Discussion started by: jacobs.smith
1 Replies

5. Shell Programming and Scripting

Replace file contents from another

Hi Friends, I have a file1 with 5 columns a b c d e f g h i j I have file2 with 3 columns 1 2 3 4 5 6 I want to replace 3rd 4th and 5th columns in file1 with file2 contents, so the output would be a b 1 2 3 f g 4 5 6 Thanks (6 Replies)
Discussion started by: jacobs.smith
6 Replies

6. Shell Programming and Scripting

sed to replace specific positions on line with file contents

Hi, I am trying to use an awk command to replace specific character positions on a line beginning with 80 with contents of another file. The line beginning with 80 in file1 is as follows: I want to replace the 000000000178800 (positions 34 - 49) on this file with the contents of... (2 Replies)
Discussion started by: nwalsh88
2 Replies

7. Shell Programming and Scripting

Replace partial contents of file with contents read from other file

Hi, I am facing issue while reading data from a file in UNIX. my requirement is to compare two files and for the text pattern matching in the 1st file, replace the contents in second file by the contents of first file from start to the end and write the contents to thrid file. i am able to... (2 Replies)
Discussion started by: seeki
2 Replies

8. Shell Programming and Scripting

Bash copy file contents into an existing file at a specific location

Hi all I need to copy the entire contents of one file into an existing file at a specific location. I know the exact line number where I need to put it. It appears I would use either sed or awk to do this, but I have been unsuccessful so far: File A line 1 line 2 line 3 line 4 ... (6 Replies)
Discussion started by: gshepherd7
6 Replies

9. Shell Programming and Scripting

Replace contents of a file

Hi, I want to replace the contents of a file.I tried using : sed 's/01514581/01514582/' $p where 01514581 is the original value 01514582 is the replaced value $p is the file name (captured in a variable).. The output does not recognise $p If you give : sed... (2 Replies)
Discussion started by: shiroh_1982
2 Replies

10. Shell Programming and Scripting

Reading specific contents from a file and appending it to another file

Hi, I need to write a shell script (ksh) to read contents starting at a specific location from one file and append the contents at specific location in another file. Please find below the contents of the source file that I need to read the contents from, File 1 -----# more... (5 Replies)
Discussion started by: dnicky
5 Replies
Login or Register to Ask a Question