I have a default conf file that we use over and over again in our projects. And, for each project, the file has to be modified. On more than one occasion, the person editing the conf file made time consuming mistakes.
So, I wanted to write a shell script that can be called to modify the conf file.
But, being new to shell scripts, I don't know how to do this. What is the appropriate *nix tool to open a text file, find a string, replace it with another and then close the text file.
Thanks!
Eric
As noted by other commenters, sed, is the typical tool.
Here's an example of an in-place (the -i option) edit of a file:
sed -i 's/Release Two/Testing Beta/g' /path/to/file.txt
You're replacing instances of the first string, Release Two, with Testing Beta everywhere in the files. The leading s says search/replace and the trailing g says do it as many times as it can be found (the default is to do it just once.) If you want to make a backup you can call
sed -iBACKUP_SUFFIX ...
You should have a look at the sed command. It allows to edit a stream (a file for example) so you can substitute, insert, remove text.
http://www.grymoire.com/Unix/Sed.html
sed
Related
Hello developer friends,
I am looking to create a Shell script to modify several configuration files at several different paths.
For example; in /etc/nginx create a .bck file of the nginx.conf file and in the .conf file, replace the value "/etc/nginx/nginx-cloudflare.conf" with "/etc/nginx/nginx-cloudflare-2022.conf"
This manipulation would have to be done on several files and I would like to automate it as much as possible.
Do you have a script with an easy way to do it?
According to my research, it would be necessary to make a loop of conditions and the use of sed
I don't really know how it works, so I'm turning to you.
I cannot comment yet due to reputation, but I was going to suggest exactly what you were thinking: create a bash shell .sh script https://www.w3schools.io/terminal/bash-tutorials/, make it executable with chmod +x filename.sh so you can run it like ./filename.sh, and within it you can use sed https://www.man7.org/linux/man-pages/man1/sed.1.html in an in-place fashion --in-place[=SUFFIX] that also creates backups of said files. Sed search replace format is 's/search/replace/flags'.
Can you please some suggest how to rename the file name using first letter of string for example this is the file i'm having in my Linux box apache-tomcat-8.2.1.
mv apache-tomcat-8.2.1 apache
this will change as per the my requirements. but in future if same line will be added inside the shell script during the time my tomcat version may be different. So here the questions is how to change the file name using first letter, like
mv ^a apache
# or
sed 's/source_file/destination_file'
can you please some one help me how to achieve this using one command, thanks.
If there's only one file, you can use a*, which means, all files begin with "a":
mv a* apache
But note that if there is more than one file that begins with "a", this won't work as expected.
I want to change my default web root folder of apache2 web server, but through command line from a script I am making.
I know to do it through nano/vim and then go to the line and change it manually, but I want to make it by a command line.
I though about some thing like (the syntax is wrong - I know - just to make my point):
vim /etc/apache2/sites-enabled/000-default.conf | find 'DocumentRoot /var/www' | replace 'DocumentRoot /var/www/myFolder'
maybe not with vim but other ??
Any Idea ?
Thanks
Use sed with argument -i.
sed -i 's-/var/www-&/MyFolder-' /etc/apache2/sites-enabled/000-default.conf
Argument -i enables in-place editing.
You should use sed with the substitute command for that kind of operation.
http://www.grymoire.com/Unix/Sed.html#uh-0
I don't have a unix machine at hand but something like that should work (using # rather than the usual / as separator):
sed 's#/var/www#/var/www/MyFolder#' /etc/apache2/sites-enabled/000-default.conf
Even if it is not your question, since your initial question mentioned Vim, you can also use substitute from inside Vim
Like
:%s #/var/war#/var/www/MyFolder#g
% means search in the whole file
g means globally : it will replace multiple instance if the string is found multiple times
I think this question fall under pipes, am bad at it.
Using one of my shell script, a file is generated with millions of rows.
Before I can use it with another command, I need to edit this file. I need to add a text e.g 'txt' in front of every line.
What i am currently doing now is,
-exit the shell script after file is generated
-open it in vim
-use command :g/^/s//txt/g to add txt at start of each line
-save file
-use it in remaining shell script
I am sure there would be a more efficient way, which i am not aware of. thanks for the help.
As some people said in the comments, you can use GNU sed to do that:
sed -i 's/^/txt/' yourfile.txt
The -i stands for --in-place and edit your file instead of printing to stdout.
I've got an irritating closed-source tool which writes specific information into its configuration file. If you then try to use the configuration on a different file, then it loads the old file. Grrr...
Luckily, the configuration files are text, so I can version control them, and it turns out that if one just removes the offending line from the file, no harm is done.
But the tool keeps putting the lines back in. So every time I want to check in new versions of the config files, I have to remove all lines containing the symbol openDirFile.
I'm about to construct some sort of bash command to run grep -v on each file, store the result in a temporary file, and then delete the original and rename the temporary, but I wondered if anyone knew of a nice clean solution, or had already concocted and debugged a similar invocation.
For extra credit, how can this be done without destroying a symbolic link in the same directory (favourite.rc->signals.rc)?
sed -i '/openDirFile/d' *.conf
this do the removing on all conf files
you can also combine the line with "find" command if your conf files are located in different paths.
Note that -i will do the removing "in place".
This was the bash-spell that I came up with:
for i in *.rc ; do
TMP=$(mktemp)
grep -v openDirFile "$i" >"$TMP" && mv "$TMP" "$i"
done
(You can obviously turn this into a one-liner by replacing the newlines with semicolons, except after do.)
Kent's answer is clearly superior.