Find the answer to your Linux question:
Results 1 to 7 of 7
Hello On the command line in linux I am trying to search some HTML code and print just a dynamic part of the code. For example this code: Code: <tr ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Oct 2012
    Posts
    4

    Searching and printing a just certain words from a line of code


    Hello

    On the command line in linux I am trying to search some HTML code and print just a dynamic part of the code. For example this code:

    Code:
    <tr class="patFuncEntry"><td align="left" class="patFuncMark"><input type="checkbox" name="renew0" id="renew0" value="i1061700" /></td><td align="left" class="patFuncTitle"><label for="renew0"><a href="/record=p1234567~S0"> the part I want to print </a></label>
    The part that changes is
    Code:
    p1234567~S0"> the part I want to print
    and I just want to print "part I want to print" The rest of the code will stay the same.

    I am trying to do this with grep but can figure out how.
    Any help would be great.
    Thanks

  2. #2
    Trusted Penguin Irithori's Avatar
    Join Date
    May 2009
    Location
    Munich
    Posts
    3,392
    hi and welcome

    My suggestion would be to use ruby and the nokogiri gem.
    Since html is already structured, you can as well take advantage of that by setting a proper css/xpath/search path via nokogiri.
    So you need not rely on a regex, that might or might not match.
    You must always face the curtain with a bow.

  3. #3
    Just Joined!
    Join Date
    Oct 2012
    Posts
    4
    Quote Originally Posted by Irithori View Post
    hi and welcome

    My suggestion would be to use ruby and the nokogiri gem.
    Since html is already structured, you can as well take advantage of that by setting a proper css/xpath/search path via nokogiri.
    So you need not rely on a regex, that might or might not match.
    Thanks for the reply.

    I am running the command as part of a python3 script using the subprocess getoutput command. I am hoping to do it something like this:
    Code:
    r = getoutput("grep comand text.txt" )
    I know very little about ruby but would there be anything like nokogiri for python3?

  4. #4
    Trusted Penguin Irithori's Avatar
    Join Date
    May 2009
    Location
    Munich
    Posts
    3,392
    For sure, a quick google search brought this:
    http://docs.python.org/py3k/library/markup.html

    Imho, executing shell commands from python is suboptimal.
    Staying within one language is a good thing:
    Less dependencies, less unexpected behaviour, more control, etc
    Last edited by Irithori; 10-06-2012 at 11:19 AM. Reason: Link to python3 instead of python2 docu
    You must always face the curtain with a bow.

  5. #5
    Just Joined!
    Join Date
    Oct 2012
    Posts
    4
    Quote Originally Posted by Irithori View Post
    Imho, executing shell commands from python is suboptimal.
    Staying within one language is a good thing:
    Less dependencies, less unexpected behaviour, more control, etc
    Are you saying that I can display HTML in python? I know I can display the source code but I mean the webpage proper?

    I cant get my head around the Python documentation. I think there should be more examples in it.

  6. #6
    Trusted Penguin Irithori's Avatar
    Join Date
    May 2009
    Location
    Munich
    Posts
    3,392
    Well, I am more of a ruby guy.
    But you can parse, modify, construct, export, etc a html or xml document in python just as well.

    As for examples: didnt look, but there are surely some to be found via google.
    You must always face the curtain with a bow.

  7. #7
    Just Joined!
    Join Date
    Oct 2012
    Posts
    4
    Quote Originally Posted by Irithori View Post
    Well, I am more of a ruby guy.
    But you can parse, modify, construct, export, etc a html or xml document in python just as well.

    As for examples: didnt look, but there are surely some to be found via google.
    Cool I will have a look.

    Thanks for your help.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •