"Sed" while changing a key in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting "Sed" while changing a key in a file
# 1  
Old 03-24-2009
"Sed" while changing a key in a file

Hi,

I am new to shell scripting.

I use Solaris10 machine.I have to write a simple shell script which replaces a part of string a file.

my file name is properties.ini, which has the following values
#application properties
location.mysql = /opt/bin # ( here location.mysql is considered as #key and /opt/bin is considered as value )
location.java = /usr/bin

Now

when i run a shell script I have to replace the value of location.mysql from /opt/bin to /opt/xyz

Currently I user the following code Which i am able to replace.

#!/bin/sh
KEY=location.mysql
VALUE=/opt/xyz
FILE=./properties.ini

sed "s|^$KEY.*|$KEY=$VALUE|g" $FILE > ./temp.bak
mv ./temp.bak $FILE

The above code is working fine, but in the code after replacing the value from location.mysql=/opt/bin to location.mysql=/opt/xyz I am moving into a file called temp.bak(temporarily) , and then moving back that file into its original name(properties.ini)

Is there any solution to replace the value in the file permanently with out moving the files.

Thanks in advance,
RaghuDeep Amilineni
raghu.amilineni
# 2  
Old 03-24-2009
If you have GNU sed, you can use the -i flag.

Else you may want to use perl. See if this works
Code:
perl -p -i -e "s|^$KEY.*|$KEY=$VALUE|g" $FILE

# 3  
Old 03-24-2009
hi vino,

Can you please explain me what is GNU sed.

I should not user perl for this code.
raghu.amilineni
# 4  
Old 03-24-2009
Quote:
Originally Posted by raghu.amilineni
hi vino,

Can you please explain me what is GNU sed.

I should not user perl for this code.
GNU sed is the sed utility available as part of the GNU Project. You can get it from GNU sed - GNU Project - Free Software Foundation (FSF)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

2. Solaris

Changing "rx_queue_number" in "ixgbe.conf". Reboot or Network Restart?

Hi all, First post here. Working on Solaris 10, on a Sun t4-4, need to change RX queue depth(ethernet, not HBA) and was wondering if i could get by with just restarting the network or if i should just bounce the whole shebang. Apologies if i missed a similar thread. if there is one, please... (2 Replies)
Discussion started by: caspnx
2 Replies

3. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

4. Shell Programming and Scripting

how to use "cut" or "awk" or "sed" to remove a string

logs: "/home/abc/public_html/index.php" "/home/abc/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" how to use "cut" or "awk" or "sed" to get the following result: abc abc xyz xyz xyz (8 Replies)
Discussion started by: timmywong
8 Replies

5. UNIX for Dummies Questions & Answers

My question is all "03" records move to output file without changing

This is my input file 01,011600033,011600033,110516,0834,2,90,,2/ 02,011600033,011103093,1,110317,0834,,2/ 03,105581,,015,+00000416418,,,901,+00000000148,,,922,+00000000354,,/ 49,+00000000000416920,00002/ 03,113806,,015,+00000559618,,,901,+00000000096,,,922,+00000000621,,/... (1 Reply)
Discussion started by: sgoud
1 Replies

6. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

7. Shell Programming and Scripting

"Join" or "Merge" more than 2 files into single output based on common key (column)

Hi All, I have working (Perl) code to combine 2 input files into a single output file using the join function that works to a point, but has the following limitations: 1. I am restrained to 2 input files only. 2. Only the "matched" fields are written out to the "matched" output file and... (1 Reply)
Discussion started by: Katabatic
1 Replies

8. Shell Programming and Scripting

Help to change the file with "sed" and "awk"

Hi experts I want your help to change the file format to my wanted version, please give me a hand thanks $cat file install pass make os pass make build kernel failed usb storage pass chane to | *install* | *make os* | *make build kernel* | *usb storage* | | pass | pass... (7 Replies)
Discussion started by: yanglei_fage
7 Replies

9. Shell Programming and Scripting

cat $como_file | awk /^~/'{print $1","$2","$3","$4}' | sed -e 's/~//g'

hi All, cat file_name | awk /^~/'{print $1","$2","$3","$4}' | sed -e 's/~//g' Can this be done by using sed or awk alone (4 Replies)
Discussion started by: harshakusam
4 Replies

10. Shell Programming and Scripting

"sed" to check file size & echo " " to destination file

Hi, I've modified the syslogd source to include a thread that will keep track of a timer(or a timer thread). My intention is to check the file size of /var/log/messages in every one minute & if the size is more than 128KB, do a echo " " > /var/log/messages, so that the file size will be set... (7 Replies)
Discussion started by: jockey007
7 Replies
Login or Register to Ask a Question