Download your favorite Linux distribution at LQ ISO.
Go Back > Blogs > Michael Uplawski
User Name


Rate this Entry

Found the old replace command

Posted 08-18-2018 at 03:48 AM by Michael Uplawski
Updated 11-13-2021 at 12:45 AM by Michael Uplawski (Category and missing sarcasm added. Orthography.)

... funny.

Discover the Norman religion:

me@this_PC:~$ echo "We can't do nothing, face it!" | replace "can't" "do not want to"
We do not want to do nothing, face it!
But the command was not available anymore. Dummy me. I have written it myself a while ago!

The script:
#!/usr/bin/env ruby
# encoding UTF-8
# This utility reads text from STDIN (via a pipe) and replaces
# therein all occurences of one string against another.
# ©2017 Michael Uplawski <>
# Use at own risk. Modify as you please.

if ARGV[0] == '-h' || ARGV[0] == '--help'
  usage = '      Syntax: cat [file] | ' << File.basename($0) << ' [string] [replacement]' 
  usage << "\n          or: " << File.basename($0) << ' [string] [replacement] << [file]'
  usage << "\n\n     Example: echo 'Quit the rebellion!' | replace \"the rebellion\" complaining"
  usage << "\n              Will display: Quit complaining!"
  usage << "\n\n\t©2017 Michael Uplawski <>"
  puts usage
  exit true

# clear ARGV prior reading the pipe
if !$<.eof? 
  out = $<.read.gsub(IN, OUT)
  puts out
... I may be programming in my sleep or am suffering from a strange variety of “selective” Alzheimer.
« Prev     Main     Next »
Total Comments 5


  1. Old Comment
    sed can do this
    $ echo "We can't do nothing, face it!" | sed -e "s/can't/do not want to/g"
    We do not want to do nothing, face it!
    Note: "can't" is a contraction of "cannot," so the above is actually not an exact restatement.
    We cannot do nothing, face it!
    Posted 08-22-2018 at 07:16 PM by jr_bob_dobbs jr_bob_dobbs is offline
  2. Old Comment
    Originally Posted by jr_bob_dobbs View Comment
    sed can do this
    I guessed something like that.
    But the last time I used sed to iron my shirts, it burned them.
    Posted 08-25-2018 at 06:17 AM by Michael Uplawski Michael Uplawski is offline
  3. Old Comment
    Originally Posted by Michael Uplawski View Comment
    I guessed something like that.
    But the last time I used sed to iron my shirts, it burned them.
    Understood. It's easy to get something wrong in sed, and sed can do a lot of damage.
    Posted 08-29-2018 at 04:54 PM by jr_bob_dobbs jr_bob_dobbs is offline
  4. Old Comment
    So I re-read this blog again.

    Addressing your "I may be programming in my sleep or am suffering from a strange variety of 'selective' Alzheimer," comment...

    Recently I started a text file with the name of each of my programs and a line or two explaining what each one does. This saves me from writing a program twice because I forgot what the first version was called! This has been a big help! You might want to do this?
    Posted 03-29-2021 at 10:19 PM by jr_bob_dobbs jr_bob_dobbs is offline
  5. Old Comment
    Funny enough, mysql-server ships with a replace command.

    $ package.whatprovides /usr/bin/replace

    I too used to forget how I named my scripts and bash functions/aliases, then I started grouping them in "packages" and make the most out of tab completion so that I don't need to remember their names anymore, one of them is the package.whatprovides function I used above. The other tools in the "package" package (very confusing name for an example) are :

    # package.<tab>
    package.count           package.files.local     package.install         package.list.all        package.list.updatable   package.whatprovides
    package.describe        package.files.remote    package.list.files      package.remove
    # package.
    I also have my "mail" utils
    # mail.<tab>                            mail.spam.rules.edit                        mail.users.password.change
    mail.alias.edit                                             mail.password.checkstrength                 mail.user.detail                            mail.users.password.change.fromcsv                         mail.queue.summary.sorted.bydate                                                                                              mail.users.password.changes                         mail.sec.failauth.expect                    mail.user.quota                             mail.users.password.changes.lastn                                   mail.sec.failauth.get                       mail.user.rename                            mail.users.password.changes.lastn.numbered
    mail.alias.remap                                                mail.user.rename.from.csv                   mail.users.password.check                                                       mail.users.count                                                      mail.sec.failcop                            mail.users.disable                
    mail.dsn.follow                             mail.hosts.sorted                           mail.spam.check.ip                          mail.users.enable                 
    mail.dsn.get                                                    mail.spam.check.ip.list                     mail.users.lastn                               mail.imap.activity.all                                          mail.users.lastn.numbered                                                                         mail.spam.rule.describe           
    # mail.

    And some handy "net" utils
    # net.<tab>
    net.connexions                                   net.connexions.whatconnectsto               net.ip.reverse                              net.port.probe.remote          net.connexions.from.application                                    net.paste  net.connexions.from.outside                 net.ip.private                                              net.ip.public                               net.port.probe.local
    # net.

    Most of them are functions and aliases with a few exceptions (for eg. when the code needs to be called from an awk script, in that case I write it as a standalone bash script).

    I also like grouping them together in their source files and adding some funny boxes around them to make them stand out.
    #           __   _,--="=--,_   __
    #          /  \."    .-.    "./  \
    #         /  ,/  _   : :   _  \/` \
    #         \  `| /o\  :_:  /o\ |\__/
    #          `-'| :="~` _ `~"=: |
    #             \`     (_)     `/
    #      .-"-.   \      |      /   .-"-.
    # .---{     }--|  /,.-'-.,\  |--{     }---.
    #  )  (_)_)_)  \_/`~-===-~`\_/  (_(_(_)  (
    # (            Network/Internet           )
    #  )                                     (
    # '---------------------------------------'
    function net.paste {
    if (($# == 0)) 
        curl -kF "clbin=<-" ""
        curl -kF "clbin=<-" "" < "$1"
    alias net.ip.private="ifdata -pa $INTERFACE"
    alias net.ip.public='curl; echo'
    function net.port.probe.local(){ _asroot lsof -i ":$1"; }
    function net.port.probe.remote(){ nmap "$1" -p "$2" ;}
    function{ grep "\b$1\b" /etc/services; }
    function net.ip.reverse(){ local IFS; IFS=.; set -- $1; echo $4.$3.$2.$1; }
    #         _\|/_
    #         (o o)
    # +----oOO-{_}-OOo-+
    # |    Packages    |
    # +---------------*/
    alias package.count='package.list | wc -l'
    function package.list.all {
        cd /var/lib/dpkg/info
        function x {
    	for filename in /var/lib/dpkg/info/* 
    	    echo ${x%.*}
        x | sort | uniq
        cd $current
    function package.list.updatable {
        function _package_description { 
    	read input;
    	dpkg -l ${input} | grep " ${input} " | awk '{$1=$2=$3=$4=$5=$6="";print $0 }' | sed 's/^ */,/';
    	unset input;
        function _updatable { 
    	apt-get --just-print upgrade 2>&1 | 
    	perl -ne 'if (/Inst\s([\w,\-,\d,\.,~,:,\+]+)\s\[([\w,\-,\d,\.,~,:,\+]+)\]\s\(([\w,\-,\d,\.,~,:,\+]+)\)? /i) {print "$1 ,$2, $3 \n"}';
        _updatable | while read -r line; do echo -en "$line $(echo $line | awk '{print $1}' | _package_description )\n"; done;
    function package.describe {
        apt-cache show "$1" | grep-dctrl -s Description-en -
    alias"dpkg-query -W"
    alias"apt-cache search"
    alias"apt-file search"
    function package.list.files { dpkg -L "$@" 2>/dev/null || apt-file list "$@"; }
    alias package.install='_asroot apt-get install'
    alias package.remove='_asroot apt-get remove'
    function package.whatprovides { dpkg -S "$1" | cut -f1 -d: | sort | uniq ; }
    Posted 07-28-2022 at 10:50 AM by ychaouche ychaouche is offline


All times are GMT -5. The time now is 05:47 PM.

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