user2031888
user2031888

Reputation: 279

Changing contents of a file through shell script

I have a requirement where in I need to change the contents of a file say xyz.cfg. the file contains values like:

group address=127.8.8.8
port=7845
Jboss username=xyz_ITR3

I want to change this content when ever needed through a shell script and save the file. Changed content can look like:

group address=127.8.7.7  
port=7822
Jboss username=xyz_ITR4

How can i achieve this using shell script by taking user input or otherwise?

Upvotes: 26

Views: 142042

Answers (6)

Vandana Sharma
Vandana Sharma

Reputation: 126

You can achieve this as follows -

File script.sh :

while [ $# -gt 0 ]
do
 case "$1" in
     --group-address)
            export NEW_VAL1=$2
            shift 2
            ;;
     --port)
            export NEW_VAL2=$2
            shift 2
            ;;
     --username)
            export NEW_VAL3=$2
            shift 2
            ;;

    *)
    echo "Unrecognized option: $1"
    usage 1
    esac
done


sed -i 's/group address=.*/group address='$NEW_VAL1'/g' xyz.cfg
sed -i 's/port=.*/port=.'$NEW_VAL2'/g' xyz.cfg
sed -i 's/Jboss username=.*/Jboss username='$NEW_VAL3'/g' xyz.cfg

You can now update these values by passing respective arguments -

  • --group-address
  • --port
  • --username

on command line to this script

For example -

./script.sh --group-address 127.8.7.7 --port  7822 --username xyz_ITR4

Upvotes: 0

guvenim
guvenim

Reputation: 91

In addition to the solutions above, you should watch out for the escape characters in the text you replacing.

For example, if you replacing something like /home/user/ then you will not get the result that you would like to get.

To solve this problem you can change the delimiter from / to |. See the code sample below.

OLD="path/to/replace"
NEW="new/path"
file=file-to-search.log

sed "s|$OLD|$NEW|g" $file

Upvotes: 4

kiran jadhav
kiran jadhav

Reputation: 21

sed -i "s/$name/$new_name/g" address.txt

This command can also be used for modifying the data.

Upvotes: 2

Yuriy Vasylenko
Yuriy Vasylenko

Reputation: 3171

*

#! /bin/sh
file=xyz.cfg
addr=$1
port=$2
username=$3
sed -i 's/address=.*/address='$addr'/' $file
sed -i 's/port=.*/port='$port'/' $file
sed -i 's/username=.*/username='$username'/' $file

*

I hope this one will be simpler to understand for beginners

Upvotes: 22

herdingofthecats
herdingofthecats

Reputation: 360

sed -i 's/something/other/g' filename.txt 

Will edit filename.txt in-place, and change the word 'something' to 'other'

I think -i may be a GNU extension though, but if it's OK for you, you can add it via find, xargs etc.

If you would like to change it in a shell script, you can take arguments on the command-line and refer to them by number, eg $1

Edit:

As per my comment, sudo_O's answer below is exactly the example that you want. What I will add is that it's common that you'll want to do such matches with multiple files, spanning subdirectories etc, so get to know find/xargs, and you can combine the two. A simple example of say changing the subnet in a bunch of .cfg files could be:

find -name '*.cfg' -print0 | xargs -0 -I {} sed -ie 's/\(192.168\)\.1/\1\.7/' {}

Note the -print0/-0 args to find/xargs (very useful for paths/filenames with spaces), and that you have to escape the capturing brackets because of the shell (same in sudo's example)

Upvotes: 11

Chris Seymour
Chris Seymour

Reputation: 85883

How about something like:

#!/bin/bash

addr=$1
port=$2
user=$3

sed -i -e "s/\(address=\).*/\1$1/" \
-e "s/\(port=\).*/\1$2/" \
-e "s/\(username=\).*/\1$3/" xyz.cfg

Where $1,$2 and $3 are the arguments passed to the script. Save it a file such as script.sh and make sure it executable with chmod +x script.sh then you can run it like:

$ ./script.sh 127.8.7.7 7822 xyz_ITR4

$ cat xyz.cfg
group address=127.8.7.7
port=7822
Jboss username=xyz_ITR4

This gives you the basic structure however you would want to think about validating input ect.

Upvotes: 42

Related Questions