2

I often use the very handy replace command on my CentOS server:

The replace utility program changes strings in place in files or on the standard input.

It's not a standalone package even on the RPM side, I think it's part of a larger utility package. Anyway, I'm wondering if there's an Ubuntu equivalent of this? I know I can faff around with grep and sed, but replace is much more convenient.

  • 1
    Can you edit and provide an example how it’s used? – dessert May 23 at 16:16
3

The mariadb-server-10.1, mysql-server-5.7 and percona-xtradb-cluster-server-5.7 packages contain versions of replace, but these versions seem to be mostly old and some are even deprecated.

The software has a website called https://replace.richardlloyd.org.uk, unfortunately the download link there is broken, but you can download the latest version (2.24 currently) as a .tar.gz file from here: Scroll all the way down and select “HTTP” on the last line. Follow these steps to install replace from this file:

  1. Extract the .tar.gz archive:

    tar xf replace-*.tar.gz
    
  2. Change into the directory:

    cd replace-*/
    
  3. The installation needs gmake, but it’s called make in Ubuntu, so create a symlink:

    sudo ln -s /usr/bin/make /usr/bin/gmake
    
  4. Run gmake:

    gmake
    
  5. Change the shebang of tests/runtests to bash (because it uses let, which sh doesn’t have):

    sed '1s/sh/bash/' tests/runtests
    
  6. Run the tests:

    gmake test
    
  7. Install the software to /usr/local/bin/replace:

    sudo gmake install
    

Once it’s installed you can remove the /usr/bin/gmake symlink as well as the replace-N.NN/ directory and the .tar.gz file:

cd .. && rm -r replace-*/ replace-*.tar.gz && sudo rm /usr/bin/gmake
  • Thanks, I think I'll get this one rather than insall the entire mysql-server package. – JimDeadlock May 23 at 16:54
  • I'm not sure what's going on but the download on that site seems to be some kind of weird .depot package. After some head-scratching I gave up and went with the other answer to extract from mariadb-server although it's version 1.4 – JimDeadlock May 24 at 19:36
  • @JimDeadlock Wow, the website’s download link is broken – I changed the answer and provided instructions to get the correct file. – dessert May 25 at 7:50
  • Thanks, I didn't spot the one at the bottom, I was messing around with those other binary files and getting all confused haha! I now have 2.24 working like a charm, awesome! – JimDeadlock May 27 at 14:33
  • I used make and it worked fine btw. – JimDeadlock May 27 at 14:37
5

This is an alternative way for replace 2.24 which is not from source using alien to install the rpm file since this is an app that is already part of Red Hat / CentOS.

Feel free to copy and paste the following lines in a terminal window.

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 

Then run the new app

$ 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.

Hope this helps!

  • Yes that is indeed the one I'm looking for, thanks. I know it's on its last legs but I'm enjoying it while I can :) – JimDeadlock May 23 at 16:53
  • @JimDeadlock Let me rewrite something here where you can extract it from a different application that doesn't scream deprecated. By the way, this answer only installs replace. You can delete the .deb file that gets downloaded when complete. – Terrance May 23 at 16:53
  • 1.4 works just fine for me, but 2.24 would be welcome. – JimDeadlock May 24 at 19:37
  • @JimDeadlock I would love to come up with something for you. It will take me a bit to find it. :) – Terrance May 24 at 19:49
  • @JimDeadlock Updated for 2.24. – Terrance May 24 at 20:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.