replace
我經常在我的 CentOS 伺服器上使用非常方便的命令:
替換實用程式會變更文件中或標準輸入上的字串。
即使在 RPM 方面,它也不是一個獨立的軟體包,我認為它是一個更大的實用程式包的一部分。不管怎樣,我想知道 Ubuntu 是否有類似的東西?我知道我可以使用grep
和閒逛sed
,但replace
更方便。
答案1
這是另一種方法,replace 2.24
它不是從原始碼使用alien
來安裝 rpm 文件,因為這個應用程式已經是 Red Hat / CentOS 的一部分。
請隨意將以下行複製並貼上到終端機視窗中。
cd /tmp
sudo apt install alien
wget https://extras.getpagespeed.com/redhat/7/x86_64/RPMS/replace2-2.24-1.el7.x86_64.rpm
sudo alien replace2-2.24-1.el7.x86_64.rpm
sudo dpkg -i replace2_2.24-2_amd64.deb
然後運行新應用程式
$ replace2
replace 2.24 (C) Richard K. Lloyd 2004 - The sane person's alternative to sed
Syntax: replace2 [-?] [-A] [-b] [-c startcol] [-d tempdir] [-e] [-f]
[-h] [-i] [-L] [-l linenum] [-m maxreplines] [-n] [-P] [-p]
[-r|R] [-s] [-T] [-t maxtimes] [-u backupsuffix] [-v]
[-w] [-x suffix [-x...]] [-z]
old_str new_str [-a old_str new_str...] [filename...]
-? displays this syntax message.
-A forces the program to assume all files are text files. Normally, the first
256 bytes are examined to auto-determine if a file is text or binary.
This option is deprecated and may be changed or removed in a future release.
-a allows extra pairs of strings to be replaced in order.
-b forces the program to assume all files are binary files.
-c startcol starts the string replace from column 'startcol' rather than the
first column.
-d specifies the temporary directory for storing modified files. If not
supplied, the default directory is $TMPDIR or, if that isn't set, /tmp.
-e makes search case-sensitive. new_str exactly replaces old_str
with no case-adjustment to new_str.
-f forces the update of files without any .cln backup.
-h indicates that replacement pairs are binary hex data.
-i interactively prompts the user to confirm if they want strings replaced in
next file. Specifying -i -i switches to prompting for each replacement.
-L follows soft-links specified on the command line.
-l linenum starts the string replacement from line 'linenum' rather than the
first line.
-m maxreplines specifies the maximum number of lines in a file that should
have string replacements.
-n displays what strings would be replaced without actually replacing them.
It also switches on verbose mode.
-P pre-pads new_str with leading spaces if it is shorter than old_str.
-p pads new_str with trailing spaces if it is shorter than old_str.
-r or -R recurses down the current directory tree (if no filenames are given),
replacing strings in all files found. Use -x to narrow the recursion.
-s relaxes the condition that old_str has to match a 'word' i.e. it replaces
old_str even if it is a substring of a 'word'.
-T retains the timestamps of the original unmodified files.
-t maxtimes states the maximum number of times a string can be replaced
in any single line of a file.
-u backupsuffix specifies the backup suffix for the unmodified file.
-v increments (switches on) verbose mode, reporting a summary of replacements
if specified once and all replacements if specified twice (i.e. -vv).
-w recursively replaces strings in all Web-related source files in the current
directory tree. Equivalent to "-r -x .html -x .htm -x .asp -x .js -x .css
-x .xml -x .xhtml -x .shtml -x .jsp -x .php -x .php3 -x .php4 -x .pl".
-x suffix specifies a case-insensitive filename suffix to look for when
recursing. Multiple -x params can be supplied and have an "or" meaning.
-z Zero-terminate any binary replacement string.
希望這可以幫助!
答案2
mariadb-server-10.1
、mysql-server-5.7
和percona-xtradb-cluster-server-5.7
軟體包包含 的版本,replace
但這些版本似乎大多是舊的,有些甚至已被棄用。
該軟體有一個網站叫https://replace.richardlloyd.org.uk,不幸的是,那裡的下載連結已損壞,但您可以.tar.gz
從以下位置下載最新版本(目前為 2.24)作為文件這裡:一直向下捲動並選擇最後一行的「HTTP」。請依照以下步驟replace
從此文件安裝:
提取
.tar.gz
存檔:tar xf replace-*.tar.gz
切換到目錄:
cd replace-*/
安裝需要
gmake
,但在Ubuntu中是呼叫的make
,所以建立一個符號連結:sudo ln -s /usr/bin/make /usr/bin/gmake
跑步
gmake
:gmake
將 shebang 更改
tests/runtests
為bash
(因為它使用let
,而sh
沒有):sed '1s/sh/bash/' tests/runtests
運行測試:
gmake test
將軟體安裝到
/usr/local/bin/replace
:sudo gmake install
安裝後,您可以刪除/usr/bin/gmake
符號連結以及replace-N.NN/
目錄和.tar.gz
檔案:
cd .. && rm -r replace-*/ replace-*.tar.gz && sudo rm /usr/bin/gmake