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?
How about something like:
Where
$1,$2
and$3
are the arguments passed to the script. Save it a file such asscript.sh
and make sure it executable withchmod +x script.sh
then you can run it like:This gives you the basic structure however you would want to think about validating input ect.
*
*
I hope this one will be simpler to understand for beginners
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:
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)