Edit file in linux script

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 

You should ask a specific question for a particular problem. Since Stack Overflow hides the Close reason from you: «Questions asking us to recommend or find a book, tool, software library, tutorial or other off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.»

6 Answers 6

#!/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.

#! /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

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

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)

Источник

Edit a file via bash script

I know its demanding but i need to create this for a friend who is noob at server management.Unfortunately i don’t have time to study bash scripting from start as my exams as near.I will use your examples to write the script.

Guys thanks for all the replies I have successfully done most of the bash script

However i have another problem Lets suppose i want to modify nginx.conf using sed We will deal with worker_processes
Now i want use sed to do the following 1.Find the FIRST occurrence of worker_processes in that conf and replace text with worker_processes 4;

Special Note Here: This is just an example. It may happen that the conf contain worker_processes 1; . This is hard part . I want a sed command that find the FIRST wHOLE match case of the word worker_processes , delete line of text where the word is found and and paste worker_processes 4; there and then save file. This is the most reliable method i though of when editing files (without nay risk of breaking any conf

Читайте также:  Oracle linux set static ip

One last suggestion I used sed -i ‘s/enabled=0/enabled=1/g’ /etc/yum.repos.d/remi.repo to change enabled =0 to enabled=1 under the [remi] section in remi.repo .However i have a feeling that it may modify all enabled=0 in that file , which will wreak the server.Can anyone suggest a better code.

Another stuff i am not sure of:P I want to edit a file that has this as Text Testing = «0»(Yes it has quotes and i need to keep it) It should be modified from Testing = «0» to Testing = «1»(with quotes)

Also i need to add some text with quotes at the end of a file with sed Like «Thanks Quanta»(with quote) For php you put a \ with echoing quotes , don’t know how it is done for bash

Another thing
I need to modify a line in a conf but i don’t remember what is the whole of text to replaced

Like its listen = something; , i want to modify it to listen = /tmp/php5-fpm.sock;

Big thumbs up for up quanta

Thanks for the awesome support guys

Источник

Оцените статью
Adblock
detector