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

Notices


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:

Code:
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:
Code:
#!/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 <michael.uplawski@uplawski.eu>
# 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 <michael.uplawski@uplawski.eu>"
  puts usage
  exit true
end

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

Comments

  1. Old Comment
    sed can do this
    Code:
    $ 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.
    Quote:
    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
    Quote:
    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
    Quote:
    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
    @uplawski
    Funny enough, mysql-server ships with a replace command.

    Code:
    $ package.whatprovides /usr/bin/replace
    mysql-server-5.5
    $

    @jr_bob_dobbs
    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 :

    Code:
    # package.<tab>
    package.count           package.files.local     package.install         package.list.all        package.list.updatable  package.search.byfile   package.whatprovides
    package.describe        package.files.remote    package.is.installed    package.list.files      package.remove          package.search.byname
    # package.
    I also have my "mail" utils
    Code:
    # mail.<tab>
    mail.access.external.live                   mail.exchange.from.outside                  mail.log.exchange.search.bysender           mail.spam.rules.edit                        mail.users.password.change
    mail.alias.edit                             mail.exchange.live                          mail.password.checkstrength                 mail.user.detail                            mail.users.password.change.fromcsv
    mail.alias.global.count                     mail.exchange.recievers.sorted              mail.queue.summary.sorted.bydate            mail.user.info                              mail.users.password.change.fromcsv.auto
    mail.alias.global.disable                   mail.exchange.report                        mail.queue.watch                            mail.user.new                               mail.users.password.changes
    mail.alias.global.enable                    mail.exchange.report.wonotifs               mail.sec.failauth.expect                    mail.user.quota                             mail.users.password.changes.lastn
    mail.alias.open                             mail.exchange.senders.sorted                mail.sec.failauth.get                       mail.user.rename                            mail.users.password.changes.lastn.numbered
    mail.alias.remap                            mail.exchange.storage.live                  mail.sec.failauth.live                      mail.user.rename.from.csv                   mail.users.password.check
    mail.alias.search                           mail.exchange.to                            mail.sec.failauth.report                    mail.users.count                            mail.users.search.loose
    mail.alias.show                             mail.hosts.connections.live                 mail.sec.failcop                            mail.users.disable                          mail.users.search.normal
    mail.dsn.follow                             mail.hosts.sorted                           mail.spam.check.ip                          mail.users.enable                           mail.users.search.strict
    mail.dsn.get                                mail.hosts.top                              mail.spam.check.ip.list                     mail.users.lastn
    mail.dsn.live                               mail.imap.activity.all                      mail.spam.live                              mail.users.lastn.numbered
    mail.dsn.report                             mail.imap.activity.live                     mail.spam.report                            mail.users.login.via.roundcube.live
    mail.exchange.from                          mail.log.exchange.search.byreceiver         mail.spam.rule.describe                     mail.users.login.via.roundcube.report
    # mail.

    And some handy "net" utils
    Code:
    # net.<tab>
    net.connexions                              net.connexions.dropped.report               net.connexions.whatconnectsto               net.ip.reverse                              net.port.probe.remote
    net.connexions.by.application.live          net.connexions.from.application             net.ip.info                                 net.paste
    net.connexions.by.application.live.numeric  net.connexions.from.outside                 net.ip.private                              net.port.name
    net.connexions.dropped.live                 net.connexions.to.outside                   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.
    Code:
    #           __   _,--="=--,_   __
    #          /  \."    .-.    "./  \
    #         /  ,/  _   : :   _  \/` \
    #         \  `| /o\  :_:  /o\ |\__/
    #          `-'| :="~` _ `~"=: |
    #             \`     (_)     `/
    #      .-"-.   \      |      /   .-"-.
    # .---{     }--|  /,.-'-.,\  |--{     }---.
    #  )  (_)_)_)  \_/`~-===-~`\_/  (_(_(_)  (
    # (            Network/Internet           )
    #  )                                     (
    # '---------------------------------------'
    
    
    function net.paste {
    if (($# == 0)) 
    then
        curl -kF "clbin=<-" "https://clbin.com"
    else
        curl -kF "clbin=<-" "https://clbin.com" < "$1"
    fi
    }
    alias net.ip.private="ifdata -pa $INTERFACE"
    alias net.ip.public='curl ifconfig.me; echo'
    function net.port.probe.local(){ _asroot lsof -i ":$1"; }
    function net.port.probe.remote(){ nmap "$1" -p "$2" ;}
    function net.port.name(){ 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 {
        current=$(pwd)
        cd /var/lib/dpkg/info
        function x {
    	for filename in /var/lib/dpkg/info/* 
    	do 
    	    x=${filename#/var/lib/dpkg/info/*}; 
    	    echo ${x%.*}
    	done
        }
        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 package.is.installed="dpkg-query -W"
    alias package.search.byname="apt-cache search"
    alias package.search.byfile="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
Advertisement
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
Open Source Consulting | Domain Registration