Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR SHELL

auto modify text in files bash

sed -i -e 's/search_string/replace_string/g' filename

# And with variables:

search_string="example"
replace_string="replacement"
file_name="filename.txt"
sed -i "s/$search_string/$replace_string/" $file_name

‘-i’ # option is used to modify the content of the original file
# with the replacement string if the search string exists in the file.

‘s’ # indicates the substitute command.

‘search_string’ # contains the string value that will be searched in the
# file for replacement.

‘replace_string’ # contains the string value that will be used to replace
# the content of the file that matches the  ‘search_string’ value.

‘filename’ # contains the filename where the search and replace willbe applied.

# The ‘awk’ command is another way to replace the string in a file,
# but this command cannot update the original file directly like the ‘sed’ command.
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #auto #modify #text #files #bash
ADD COMMENT
Topic
Name
8+5 =