Replace only if string exists in current line
Solution
Assuming your input file $target contains the following:
some text mystring some other text
some text mystring a searchstring
just some more text
This command:
sed -i -e '/searchstring/ s/mystring/1/ ; /searchstring/! s/mystring/0/' $target
will change its content to:
some text 0 some other text
some text 1 a searchstring
just some more text
Explanation
The script contains two substitute (s) commands separated by a semicolon.
The substitute command accepts an optional address range that select which lines the substitution should take place.
In this case regexp address was used to select lines containing the searchstring for the first command; and the lines that do not contain the searchstring (note the exclamation mark after the regexp negating the match) for the second one.
Edit
This command will perform better and produce just the same result:
sed -i -e '/searchstring/ s/mystring/1/ ; s/mystring/0/' $target
The point is that commands are executed sequentially and thus if there is still a mystring substring in the current line after the first command finished then there is no searchstring in it for sure.
Kudos to user946850.
Replace whole line containing a string using Sed
You can use the change command to replace the entire line, and the -i
flag to make the changes in-place. For example, using GNU sed:
sed -i '/TEXT_TO_BE_REPLACED/c\This line is removed by the admin.' /tmp/foo
Bash creating a string, if string already exists replace it
if there is the few text, it should replace it, if few text is missing, just create it on the first line.
That's an if.
if <file has text>; then <replace text>; else <add text to first line>; fi
or in bash:
file=/usr/local/sbin/.myappenv
if grep -q few "$file"; then
sed 's/few/asd/' "$file"
else
{
echo asd
cat "$file"
} > "$file".tmp
mv "$file".tmp "$file"
fi
How to test if string exists in file with Bash? https://unix.stackexchange.com/questions/99350/how-to-insert-text-before-the-first-line-of-a-file and https://mywiki.wooledge.org/BashGuide/TestsAndConditionals . You might interest yourself in some automation methods, like ansible lineinfile or chezmoi depending on the goal.
How to swap text based on patterns at once with sed?
Maybe something like this:
sed 's/ab/~~/g; s/bc/ab/g; s/~~/bc/g'
Replace ~
with a character that you know won't be in the string.
sed edit file in place
The -i
option streams the edited content into a new file and then renames it behind the scenes, anyway.
Example:
sed -i 's/STRING_TO_REPLACE/STRING_TO_REPLACE_IT/g' filename
while on macOS you need:
sed -i '' 's/STRING_TO_REPLACE/STRING_TO_REPLACE_IT/g' filename
Related Topics
Auto Exit Telnet Command Back to Prompt Without Human Intervention ^] Quit Close Exit Code 1
How to Set Up My Linux X Terminal So That Emacs Has Access to 256 Colors
Rename Multiple Directories Matching Pattern
Output the 2Nd Column of a File
How to Get Debugging Symbols Working in Linux Perf Tool Inside Docker Containers
What Unit Is Used to Display Redis CPU Usage
Null Modem Emulator (Com0Com) for Linux
Insert Linux Kernel Module Statically
Unable to Start Rstudio in Centos Getting Error "Unable to Connect to Service"
Remove ^H and ^M Characters from a File Using Linux Shell Scripting
Bash Print Stderr Only, Not Stdout
How to Load a Specific Version of R in Linux
Running Shell Script Using .Env File
Will Ctrl+C Send Sigint Signals to Both Parent and Child Processes in Linux
How to Find Which Process Is Leaking Memory
Shell Script Function Return a String
How to Detect a Buffer Over Run on Serial Port in Linux Using C++