LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-06-2019, 02:34 AM   #1
vijaytamil2000
LQ Newbie
 
Registered: Nov 2019
Posts: 5

Rep: Reputation: Disabled
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.

[wininstall]
server1fqdn: server1.com
server1comps: ifwe=False ifge=False ifze=False

server2fqdn: server2.com
server2comps: ifwe=False ifge=True ifze=False

[endwininstall]

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'

output:

ifge=False

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

Can any help me out this.
 
Old 12-06-2019, 02:44 AM   #2
scasey
Senior Member
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.7.1908
Posts: 4,267

Rep: Reputation: 1495Reputation: 1495Reputation: 1495Reputation: 1495Reputation: 1495Reputation: 1495Reputation: 1495Reputation: 1495Reputation: 1495Reputation: 1495
see man sed and search for examples on the 'net
Code:
sed -i.bak 's/pattern_to_match/replacement/ filename
You don't need any other commands.
 
1 members found this post helpful.
Old 12-06-2019, 04:02 AM   #3
Turbocapitalist
Senior Member
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 4,384
Blog Entries: 3

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

Code:
/startpattern/,/stoppattern/ { ... }
Make sure your sed script does what you need it to do before you use the -i option.
 
1 members found this post helpful.
Old 12-06-2019, 10:27 PM   #4
vijaytamil2000
LQ Newbie
 
Registered: Nov 2019
Posts: 5

Original Poster
Rep: Reputation: Disabled
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.

ex:

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?
 
Old 12-06-2019, 10:36 PM   #5
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 18,396

Rep: Reputation: 3048Reputation: 3048Reputation: 3048Reputation: 3048Reputation: 3048Reputation: 3048Reputation: 3048Reputation: 3048Reputation: 3048Reputation: 3048Reputation: 3048
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
Code:
sed '/\bserver2comps\b/ ...' test.text
Replace the ellipses with your substitution command.

Last edited by syg00; 12-06-2019 at 10:51 PM. Reason: added code
 
1 members found this post helpful.
Old 12-07-2019, 03:06 AM   #6
Turbocapitalist
Senior Member
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 4,384
Blog Entries: 3

Rep: Reputation: 2184Reputation: 2184Reputation: 2184Reputation: 2184Reputation: 2184Reputation: 2184Reputation: 2184Reputation: 2184Reputation: 2184Reputation: 2184Reputation: 2184
Or combining two of the replies:

Code:
sed -r -e '/^.wininstall.$/,/^.endwininstall.$/{
        /\bserver2comps\b/ s/.../.../;
}'
 
Old 12-07-2019, 07:37 AM   #7
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 1,267

Rep: Reputation: 580Reputation: 580Reputation: 580Reputation: 580Reputation: 580Reputation: 580
In perl a (match group) is referred as $1 or ${1}
Code:
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
Code:
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.
Code:
sed -i.bak -r '/^\s*server2comps:/ s/(ifge=)True/\1False/' test.txt

Last edited by MadeInGermany; 12-07-2019 at 07:44 AM.
 
Old 12-09-2019, 05:09 AM   #8
vijaytamil2000
LQ Newbie
 
Registered: Nov 2019
Posts: 5

Original Poster
Rep: Reputation: Disabled
Thanks every one for your help
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
changing files extensions using inplace edit (perl) Alena Linux - Newbie 3 10-04-2017 02:48 PM
[SOLVED] Migrate From CentOS-6 to CentOS-7 - Not Inplace etpoole60 Linux - Software 3 04-24-2016 08:50 AM
inplace upgrade of RH 6.6 to RH 7 raggmopp1961 Linux - General 3 05-20-2015 07:56 AM
How to redirect standard stdout to multi stdout ( Bash )? john.daker Programming 4 11-04-2008 12:20 AM
redirecting stdout to /dev/null and stderr to stdout? Thinking Programming 1 05-18-2006 03:36 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:46 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration