Find and replace nth instance of text string using python or bash

07
2014-07
  • user3772744

    I have a configuration file that I am trying to edit.

    For example the file says : listen = 1.1.1.1 and I want it to say listen = 2.2.2.2.

    However the word listen occurs in other places in the file. So I want to know how to read in the file and only change the nth instance

  • Answers
    Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

    Related Question

    remotely execute bash or python scripts via webserver on ubuntu
  • gletscher

    Hi I want to run certain scripts on my linux machine by simply clicking a link on a webpage that is supplied by a webserver running on that machine. It is currently running ubuntu, which webserver would be the easiest to configure such a setup? Do I have to use cgi or is there a faster / simpler way?


  • Related Answers
  • Rich Homolka

    As the above answers have mentioned, there are a few ways of doing this. Apache has been the standard webserver for Linux for a long time. It's easy to use it, and others on your ubuntu server.

    What scripts are you trying to run? Are you exposing to the wild Internet or just local users? I'd be very wary of opening any of this up to the Internet. You'd then need to track webserver versions, updating as security patches come down. Even more important are your scripts. You can easily open holes to your machine. You have to be super diligent when checking and sanitizing your inputs. Your authentication on the script may leak info about users on the machine.

    Depending on what you're doing, I might look for a package that already does what you want to do. For generic sysadmin stuff, i'd check out Webmin. http://www.webmin.com/download.html

  • Nifle

    Most web servers can do that.

    But if you are familiar with perl (or want to be) give HTTPi a try.

    • small footprint: one single small script with no additional Perl modules required; you can get away with just the Perl executable all by itself with no libraries at all
    • full features: executables/scripts, server-parsed pages, user filesystems, HTTP authentication, IP/agent security, and even IP-less and IP-based virtual hosting/multihoming
    • fast response time, configurable for even more speed
    • security: highly secure, rigourously tested
  • gorilla

    CGI is the simplest way of doing it. There are various faster ways, but they're going to be more complex, either to setup, or in programming.