-   Programming (
-   -   searching sed inplace from stdout (

vijaytamil2000 12-06-2019 02:34 AM

searching sed inplace from stdout
Hello Folks,

I am searching a command where i want to write the replace string into files.

Below are the scenarios.

I have file called test.txt and the below are the contents.

server1comps: ifwe=False ifge=False ifze=False

server2comps: ifwe=False ifge=True ifze=False


Now i am trying to replace 'ifge=True' to false in the text.txt file.

I tried the below commanad:

cat test.txt | grep -w server2comps | cut -d ":" -f2-14 | grep -oP 'ifge[^[:blank:]]*' | sed 's/True/False/g'



But my question is how can write this changes in file directly using 'i' option.

Can any help me out this.

scasey 12-06-2019 02:44 AM

see man sed and search for examples on the 'net

sed -i.bak 's/pattern_to_match/replacement/ filename
You don't need any other commands.

Turbocapitalist 12-06-2019 04:02 AM

Additionally, one syntax which may help will be the ability to work on a span of text between to markers:


/startpattern/,/stoppattern/ { ... }
Make sure your sed script does what you need it to do before you use the -i option.

vijaytamil2000 12-06-2019 10:27 PM

I guess its not a normal sed , sed -i requires an input file but in my case i need to pass all the serach pattern queries into substitude param.


sed -i '/s/$(cat test.txt | grep -w server2comps | cut -d ":" -f2-14 | grep -oP 'ifge[^[:blank:]]*')/False/g'

So is there any way to achieve this?

syg00 12-06-2019 10:36 PM

That is just so ugly. Do it all in one sed command - none of the other commands, including cat, are needed. sed has all the regex, all the string matching you need. All of it - see the doco on the gnu site, not the manpage.
For example, this will should do the selection for you

sed '/\bserver2comps\b/ ...' test.text
Replace the ellipses with your substitution command.

Turbocapitalist 12-07-2019 03:06 AM

Or combining two of the replies:


sed -r -e '/^.wininstall.$/,/^.endwininstall.$/{
        /\bserver2comps\b/ s/.../.../;

MadeInGermany 12-07-2019 07:37 AM

In perl a (match group) is referred as $1 or ${1}

perl -i.bak -pe 's/^(\s*server2comps:.*ifge=)True/${1}False/' test.txt
.* is "any characters".
\s* is "white space".
^ is the beginning of the line.
In sed a (match group) is referred as \1
The following needs GNU sed

sed -i.bak -r 's/^(\s*server2comps:.*ifge=)True/\1False/' test.txt
The -i.bak renames to test.txt.bak then writes the output to a new test.txt file.
As was suggested before, an initial filter (line address) allows to capture/replace less.

sed -i.bak -r '/^\s*server2comps:/ s/(ifge=)True/\1False/' test.txt

vijaytamil2000 12-09-2019 05:09 AM

Thanks every one for your help

All times are GMT -5. The time now is 03:18 PM.