Python Forum
Python equivalent to sed [solved]
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python equivalent to sed [solved]
#1
I'm trying to create a program guide for BBC Radio 4 Extra.
I'm using python3.4 and Debian linux on Raspberry Pi

In linux the stream editor (sed) can extract content using

sed -n '/14:00/, +3p' mon

which will extract from program guide (mon) at time 14:00 and display
next 3 lines +3p

In python3 I've redirected stdout to a variable called 'content' which works
but cannot find a way to make the time a variable. Code used

insert os

content = os.popen("sed -n '/14:00/, +3p' mon").read()
This gives the same results and content gets the value of stdout.
However if I wanted to change time (14:00) I cannot pass this as a variable

e.g.
time ="14:00"
content = os.popen("sed -n '/time/, +3p' mon").read()

fails to extract any content.

There is possibly more than one way to do this, and there may be some function
in the python standard library.
I can't find a way to attach the file called 'mon'
Truncated content below:


Output:
14:00 Book at Bedtime—James Bond - Solo, Episode 6 6/10 His mission in Dahum violently interrupted, Bond goes home to plan revenge. 7. 14:15 The Making of Music—Series 1, Bach's St Matthew Passion 16/30 Johann Sebastian's recurring choral theme had a profound effect on the history of music. 8. 14:30 Maggie Allen - Not Me, But Us—Beginnings 1/10 The story of a medical pioneer who campaigned for women's education in the 19th century. 9.
Thanks in advance.
Reply
#2
(Sep-24-2018, 02:32 PM)cygnus_X1 Wrote: time ="14:00"
content = os.popen("sed -n '/time/, +3p' mon").read()


It would be a crazy world indeed, if all your variables were just injected into strings. You need to specifically let python know in some way that you expect interpolation to be taking place:
>>> time = "14:00"
>>> "spam /time/ eggs"
'spam /time/ eggs'
>>> "spam /{0}/ eggs".format(time)
'spam /14:00/ eggs'
>>> f"spam /{time}/ eggs"
'spam /14:00/ eggs'
Reply
#3
Thanks for your reply Nilamo, I see my mistake now.

content = os.popen("sed -n '/time/, +3p' mon").read()

The solution I used was string concatenation:

str1 = "sed -n '/" + str(time)+ "/, +3p' mon"

content = os.popen(str1).read()
It may not be the best or indeed the only solution but it
works for my purpose.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Equivalent Python code from VBA Mishal0488 1 633 Jul-05-2023, 10:43 AM
Last Post: carecavoador
  Python C++ Template Equivalent deanhystad 7 3,313 May-04-2021, 07:45 PM
Last Post: deanhystad
  Hi Guys, please help me to write SAS macro parameter equivalent code in Python Manohar9589 2 2,540 Jun-14-2020, 05:07 PM
Last Post: Larz60+
  python equivalent to MATLAB xcov chai0404 2 3,822 Apr-02-2020, 10:29 PM
Last Post: chai0404
  Python equivalent of Matlab code kwokmaster 1 3,400 Mar-25-2020, 10:14 PM
Last Post: j.crater
  Equivalent of 'bwareafilt' for Python Mighty 1 3,543 Feb-05-2020, 10:32 PM
Last Post: Marbelous
  Convert a mongo db scrip to python equivalent Herath 1 2,163 Aug-20-2019, 02:28 PM
Last Post: fishhook
  SAS Equivalent of Macro variable in Python AI_ML_Neophyte 4 11,527 Mar-29-2019, 08:14 AM
Last Post: FelixS
  equivalent commands saeed_balk 2 2,613 Sep-23-2018, 05:44 AM
Last Post: saeed_balk
  Python "read -n" equivalent? rhubarbpieguy 8 6,679 Jan-24-2017, 12:51 PM
Last Post: rhubarbpieguy

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020