searched through the forums and not really found what I am looking for. I am a bit of novice when it comes to anything above basic scripting and not even that when it comes to the sed command.
I have been reading the tutorials online but still struggling to get what I need :wall:
I have a script that has the following output (a small example of the full file):
What I want to do is replace the "_" with a space between the numbers and the server names e.g.
from:
1639163_Cluster11_Prodto:
1639163 Cluster11_Prod
now I have been able to do this by using the command:
sed 's/1639163_/1639163 /g' example.txt
I am not sure if there is a better way of doing this but if there is not how do I do a multiple search and replace for the other sized devices within the one command - e.g.:
sed 's/2048953_/2048953 /g' example.txt
sed 's/1228838_/1228838 /g' example.txt
sed 's/1024477_/1024477 /g' example.txt
etc, etc.
I also want the changes to update the example.txt file - is this possible?
However, sed does not allow you to edit in place, you will have to do something like:
sed .... oldfile > newfile
mv newfile oldfile
If I were writing the script, I would do:
#! /bin/ksh
for file in "${@}"; do
temp=$(mktemp --tmpdir=$(dirname ${file}))
sed -e 's/..../..../' ${file} > ${temp}
mv ${temp} ${file}
done
Now using man mktemp (linux) may be a bit of overkill, but it guarantees that you are not overwritting an existing file. And the --tmpdir option ensures the man mv (linux) does not have to copy the file between filesystems.
as per my understanding if you are looking for multiple replacement and change in a single command this shall work
sed -e "s/cat/lion/g" -e "s/yes/no/g' -e 's/true/false/g' -e ....... -i.backup actual_file
will do all the replacement and keep a backup of your file, named actual_file.backup
offcourse its up to you, how good you are able to write the regular expression, more accurate less errors and surprises, but it will be fun, welcome to sed
its tricky first time, try to keep a backup manually