all files have a line that reads: "Depricated funcstions="
Currently, this line is blank after the "=" in all the files.
assuming I need to enter the same text to all files (For example I want all files to be edited so that the lines now reads:
"Depricated dunctions=foo, bar"
but the row number in which the line appears is different for each file.
is there a way to achieve that for all file.conf files in /home/functions/verXX/file.conf?
Shell ScriptingLinux
Last Comment
David Sankovsky
8/22/2022 - Mon
Jesse Bruffett
you can use sed to do this. i would recommend writing a script to do it. this page pretty much explains the command in terms of how it works and proper syntax. https://www.gnu.org/software/sed/manual/sed.html
the script is important because sed will read the files you tell it to, make the changes and then output the changed data to the location you tell it. ive found its best to send them to a temp location then move them into production. also you can use the script to make backups of the originals in case something happens and you need them.
Please note that actual files will be changed by this approach and all lines with string "Depricated functions" , please back up files before testing
tel2
Not bad, Abhimanyu, but here are some tips for possibly improving your answer slightly:
Looking at this command: sed -i'' 's/Depricated functions=/Depricated functions=sed/g' /home/ver*/file.conf
The -i switch doesn't need '' after it.
Your comment about backing up files first could have been handled by providing an alternative solution which did that automatically, e.g.
sed -i.bak ...
You don't need to repeat the "Depricated functions=" match. An "&" can be used on the right hand side of the substitution to repeat the entire left hand side.
sed's "/g" modifier is required only if you want to replace multiple matches per line, so shouldn't be needed here.
You are already in the /home directory, so you don't need to start the path with /home (but I know there may be reasons for doing so). Also, the asker's files are in /home/functions/ver...
So, your command could be reduced to something like this:
sed -i 's/Depricated functions=/&sed/' ver*/file.conf
Or if you want to create backup files and name them file.conf.bak:
sed -i.bak 's/Depricated functions=/&sed/' ver*/file.conf
Also, your use of "ls -ltrh" and "ls -ltr" could probably just be simplified to "ls -l" in cases like this. I don't think the extra switches are adding value here.
the script is important because sed will read the files you tell it to, make the changes and then output the changed data to the location you tell it. ive found its best to send them to a temp location then move them into production. also you can use the script to make backups of the originals in case something happens and you need them.