LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 08-01-2019, 12:48 PM   #1
ozsnake
LQ Newbie
 
Registered: Jun 2015
Location: Mauritius
Distribution: CentOS, MacOS, Ubuntu
Posts: 8

Rep: Reputation: Disabled
Question How to run 'sed' using Python's Subprocess?


I'm trying to append some data after a matching pattern with sed. In bash it works fines. When i tried to run the same command in a python script, nothing is being appended.

The file's content:

Code:
# directive as shown below:
This is a configuration file.

In bash when i run sed, it works fine:

Code:
[root@centos ~]# sed -i '/^# directive* /a cfg_dir=\/etc\/nagios\/commands' count

[root@centos ~]# cat count
# directive as shown below:
cfg_dir=/etc/nagios/commands
This is a configuration file.

The python code:

Code:
test = ['command', 'local', 'test']

os.chdir('/root')

for t in test:
    sp.run(f"sed -i '/^# directive shown below* /a cfg_dir=\/etc\/nagios\/testing' count", shell=True)
Nothing's changed:

Code:
[root@centos ~]# python3 test.py

[root@centos ~]# cat count
# directive as shown below:
This is a configuration file.
I just don't get it.
 
Old 08-01-2019, 02:14 PM   #2
ozsnake
LQ Newbie
 
Registered: Jun 2015
Location: Mauritius
Distribution: CentOS, MacOS, Ubuntu
Posts: 8

Original Poster
Rep: Reputation: Disabled
Misspelled Error.

The solution:

for t in test:
sp.run(f"sed -i '/^# directive* /a cfg_dir=\/etc\/nagios\/{t}' count", shell=True)
 
Old 08-01-2019, 08:03 PM   #3
individual
Member
 
Registered: Jul 2018
Posts: 231

Rep: Reputation: 175Reputation: 175
Is there any reason you couldn't write that sed statement in Python? It would be more efficient that having to create a subprocess just for for that.
 
Old 08-04-2019, 12:33 PM   #4
ozsnake
LQ Newbie
 
Registered: Jun 2015
Location: Mauritius
Distribution: CentOS, MacOS, Ubuntu
Posts: 8

Original Poster
Rep: Reputation: Disabled
Yeah you're right. A simple os.system would've been enough.
 
  


Reply

Tags
centos7, python3, sed bash, sed regex


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
Python's subprocess.run() method is held up by stdout MALDATA Programming 5 09-06-2017 10:10 AM
[SOLVED] python subprocess not working inside python script divyashree Programming 1 10-01-2014 07:33 AM
Subprocess in Python giving error B Akshay Programming 7 02-14-2013 10:07 AM
python os.system() or subprocess.Popen() TypeError on ubuntu lucid 10.04 sahar.gh Programming 1 06-25-2012 07:56 PM

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

All times are GMT -5. The time now is 11:32 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