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.)
Updated 11-13-2021 at 12:45 AM by Michael Uplawski (Category and missing sarcasm added. Orthography.)
... funny.
Discover the Norman religion:
But the command was not available anymore. Dummy me. I have written it myself a while ago!
The script:
... I may be programming in my sleep or am suffering from a strange variety of “selective” Alzheimer.
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!
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
Total Comments 7
Comments
-
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!
Quote:We cannot do nothing, face it!Posted 08-22-2018 at 07:16 PM by jr_bob_dobbs -
Posted 08-25-2018 at 06:17 AM by Michael Uplawski -
Posted 08-29-2018 at 04:54 PM by jr_bob_dobbs -
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 -
@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.
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 -
Quote:So I re-read this blog again.
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?
For functions, I have some bash functions in a file following a simple "not-even-markup" code-formatting, and one of those functions lists all functions, "lsfunctions," printing things in a "formatted" way, reading from the functions file itself :
Code:#!/bin/bash bold=$(tput bold) normal=$(tput sgr0) lsfunctions () { tail -n+10 /$HOME/scripts/functions | grep "()" | sort| sed "s|(.*#||;s|^|${bold}|;s|\ |${normal}\ |" } cblowercase() # converts clipboard to lowercase { xclip -o | tr '[:upper:]' '[:lower:]' | xsel -i } [...]
Code:$ lsfunctions cblowercase converts clipboard to lowercase cbuppercase converts clipboard to uppercase c - command line calculator, bc. cdc cd $clipboard [...]
"tail -n+10" is limitting the following grep to everything after the first ten lines of the "functions" file, since there's little point in have lsfunctions explaining itself.
Not sure it if was my idea to begin with, I probably copied it from someone else, maybe in this very forum.
I think one can even come up with a fancier parser, expanding things like "\n" into newlines and whatnot.
It's perhaps also not that hard to come up with some function that would do this kind of "homemade apropos" thing but for user scripts, given that they also have some kind of header that the function parses, and that they're on a specific location, I guess.Posted 07-03-2023 at 03:59 PM by the dsc -
nice one @the_dsc
Posted 07-04-2023 at 05:15 AM by ychaouche