Find String and Replace Line in Linux

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

Find string and replace line in linux

sed 's/^volume =.*/volume = 0/g' file.txt

Replace one substring for another string in shell script

To replace the first occurrence of a pattern with a given string, use ${parameter/pattern/string}:

#!/bin/bash
firstString="I love Suzi and Marry"
secondString="Sara"
echo "${firstString/Suzi/"$secondString"}"
# prints 'I love Sara and Marry'

To replace all occurrences, use ${parameter//pattern/string}:

message='The secret code is 12345'
echo "${message//[0-9]/X}"
# prints 'The secret code is XXXXX'

(This is documented in the Bash Reference Manual, §3.5.3 "Shell Parameter Expansion".)

Note that this feature is not specified by POSIX — it's a Bash extension — so not all Unix shells implement it. For the relevant POSIX documentation, see The Open Group Technical Standard Base Specifications, Issue 7, the Shell & Utilities volume, §2.6.2 "Parameter Expansion".

find matching text and replace next line

This might work for you (GNU sed):

sed '/<key>ConnectionString<\/key>/!b;n;c<string>changed_value</string>' file

!b negates the previous address (regexp) and breaks out of any processing, ending the sed commands, n prints the current line and then reads the next into the pattern space, c changes the current line to the string following the command.

How to replace a string in multiple files in linux command line

cd /path/to/your/folder
sed -i 's/foo/bar/g' *

Occurrences of "foo" will be replaced with "bar".

On BSD systems like macOS, you need to provide a backup extension like -i '.bak' or else "risk corruption or partial content" per the manpage.

cd /path/to/your/folder
sed -i '.bak' 's/foo/bar/g' *

How to replace a whole line with sed?

Try this:

sed "s/aaa=.*/aaa=xxx/g"

Using sed to find a string and replace that line but ignore commented out lines

For file like this:

# server string = is the equivalent of the NT Description field
server string = 145000web SAMBA1
#server string = is the equivalent of the NT Description field
server string = 999999web2 SAMBA2
# server string = another comment
# server string = more comments
server string = some value SAMBA3
server string = some value SAMBA4

and with a variable like this

echo $variable
111111www5

This will do the job with gnu sed, but white space is not preserved:

sed 's/^[^#]*server string \=.*/server string = '"$variable"' SAMBA/g' file
# server string = is the equivalent of the NT Description field
server string = 111111www5 SAMBA
#server string = is the equivalent of the NT Description field
server string = 111111www5 SAMBA
# server string = another comment
# server string = more comments
server string = 111111www5 SAMBA
server string = 111111www5 SAMBA

To preserve white space you can use something like this:

sed -r 's/(^[^#]*)server string \=.*/\1server string = '"$variable"' SAMBA/g' file

or even

sed -r 's/(^[^#]*server string \=).*/\1'"$variable"' SAMBA/g' file

PS: I have ommit -i switch on purpose. You can add it when you are happy with the results.



Related Topics



Leave a reply



Submit