Generic shell function for modifying files in-place

inplace() { eval F=\"\$$#\"; "$@" > "$F".new && mv -f "$F".new "$F"; }
Some commands (such as sed and perl) have options to support in-place editing of files, but many commands do not. This shell function enables any command to change files in place. See the sample output for many examples. The function uses plain sh syntax and works with any POSIX shell or derivative, including zsh and bash.
Sample Output
$ inplace nl textfile
(number lines of textfile in-place)
$ inplace awk '{print NF,$0}' somefile
(adds field counter to a file)
$ inplace nroff -man page.1
(convert manpage source to readable "catpage")
$ inplace sort textfile
(sort the lines of a file in-place)
$ inplace sed s/foo/bar/g myfile
(This just serves as an example, sed has an -i option for in-place editing anyway)

1
By: inof
2010-04-09 11:36:31

These Might Interest You

  • Remove old kernels (*-generic and *-generic-pae) via apt-get on debian/ubuntu based systems. Tested on ubuntu 10.04 - 12.04.


    3
    sudo apt-get remove $(dpkg -l|awk '/^ii linux-image-/{print $2}'|sed 's/linux-image-//'|awk -v v=`uname -r` 'v>$0'|sed 's/-generic*//'|awk '{printf("linux-headers-%s\nlinux-headers-%s-generic*\nlinux-image-%s-generic*\n",$0,$0,$0)}')
    mtron · 2012-08-15 10:02:12 1
  • Note the double space: "...^ii␣␣linux-image-2..." Like 5813, but fixes two bugs: [1]This leaves the meta-packages 'linux-headers-generic' and 'linux-image-generic' alone so that automatic upgrades work correctly in the future. [2]Kernels newer than the currently running one are left alone (this can happen if you didn't reboot after installing a new kernel). I'm bummed that this took 228 characters. I'd like to see a simpler version. Show Sample Output


    7
    aptitude remove $(dpkg -l|awk '/^ii linux-image-2/{print $2}'|sed 's/linux-image-//'|awk -v v=`uname -r` 'v>$0'|sed 's/-generic//'|awk '{printf("linux-headers-%s\nlinux-headers-%s-generic\nlinux-image-%s-generic\n",$0,$0,$0)}')
    __ · 2010-12-11 11:38:15 3
  • Same as 7272 but that one was too dangerous so i added -P to prompt users to continue or cancel Note the double space: "...^ii␣␣linux-image-2..." Like 5813, but fixes two bugs: [1]This leaves the meta-packages 'linux-headers-generic' and 'linux-image-generic' alone so that automatic upgrades work correctly in the future. [2]Kernels newer than the currently running one are left alone (this can happen if you didn't reboot after installing a new kernel).


    -1
    sudo aptitude remove -P $(dpkg -l|awk '/^ii linux-image-2/{print $2}'|sed 's/linux-image-//'|awk -v v=`uname -r` 'v>$0'|sed 's/-generic//'|awk '{printf("linux-headers-%s\nlinux-headers-%s-generic\nlinux-image-%s-generic\n",$0,$0,$0)}')
    Bonster · 2011-04-25 05:19:57 0
  • Running this command turns shell tracing and shell verbose debugging on or off. Not only does it do that, it also uses your terminals builtin method of setting colors to make debugging much easier. It looks at the current shell options contained in the $- special bash variable and that lets this function set the opposite of the current value. So from the shell you could do a: setx; echo "y" | ( cat -t ) | echo "d"; setx and it will turn on debbuggin. This is an amazingly useful function that is perfect to add system-wide by adding it to /etc/profile or /etc/bashrc.. You can run it from the shell, and you can also use it in your shell scripts like my .bash_profile - http://www.askapache.com/linux-unix/bash_profile-functions-advanced-shell.html Show Sample Output


    1
    function setx(){ sed '/[xv]/!Q2' <<< $- && { set +xv; export PS4=">>> "; } || { export PS4="`tput setaf 3`>>> `tput sgr0`"; set -xv; }; }
    AskApache · 2010-02-14 01:25:44 1

What Others Think

Very nice. Of course, you can shorten it by a couple of chars :-) inplace() { eval F=\"\$$#\"; "$@" > "$F".new && mv -f "$F"{.new,}; }
flatcap · 424 weeks and 1 day ago
flatcap: If you shorten it that way, it doesn't work anymore in plain (POSIX) bourne shells (it still works with zsh and bash, though). Personally I prefer it to be as portable as possible, even if it is two characters longer. That way I can easily share my collection of shell functions between different machines (BSD, Linux, Solaris). If you absolutely need to make it shorter, you can omit several of the spaces that I left for better readability. That way you can save more than just two characters, and it's still portable. ;-)
inof · 424 weeks and 1 day ago
FYI I'm working on an inplace script for coreutils that uses some of the techniques mentioned here: http://www.pixelbeat.org/docs/unix_file_replacement.html
pixelbeat · 423 weeks and 5 days ago
Ah-ha. Now I see why you downvoted my sedw function. I had not seen this one you wrote. You have some cool stuff; your shell skills are better than mine. But if we're playing keystroke golf, I think sedw 's/old/new/' beats inplace sed 's/old/new/' Beginner's luck. :)
argv · 422 weeks and 6 days ago
argv: I haven't voted on your sedw function (neither up nor down), if I recall correctly. But still, all the major BSDs have -i options for in-place editing, so there's no need in this case. Well, you could make an alias sedw="sed -i" if you want to save two keystrokes. ;-)
inof · 422 weeks and 6 days ago
I just presumed it was you. I will find that guy who voted me down! ;) NetBSD's sed does not have -i. This is why I made the sedi and sedw functions. Because the sed in NetBSD base does not have in-place editing. I used aliases in the early days but now prefer functions. They allow more flexibility and can be used in scripts. I like your approach to in-place editing; it's simple, verstile and portable. I opted to use /tmp because that's what ed and the newer sed's do. They used /tmp for in-place editing. If your sed has an -i option, it's making a tmp file and then removing it. I always have /tmp mounted as mfs or tmpfs. Cheers.
argv · 422 weeks and 6 days ago
argv: Using /tmp is a bad idea, actually, for security reasons. World-writable directories can be used for several kinds of attacks, for example symlink attacks. Even if you use '$$' there is a race condition that can be abused. Some systems provide tools like mktemp(1), but this is not portable. Therefore I try to avoid using /tmp at all, especially on multi-use machines (shell boxes). By the way, FreeBSD's sed doesn't use /tmp either. It creates a backup file in the same directory as the file being edited. I don't know what GNU sed does (I'm too lazy right now to log into a Linux machine for checking), but I guess it's also clever enough to not use /tmp. Many programs recognize the environment variable TMPDIR, so I always set it to a directory that is not world-writable, like TMPDIR=$HOME/tmp (which is 0700, i.e. drwx------).
inof · 422 weeks and 6 days ago
Thanks inof. If one is in a multi-user environment and truly paranoid about symlink-racing, then yes, avoid /tmp. And check all your applications as some of them may use it. It was GNU sed I was thinking of that uses /tmp. I'm too lazy to install it or boot into a Linux to double check. I know ed uses /tmp. And vi uses /var/tmp for vi.recover, correct? I presumed sed's with in-place editing would use /tmp. As usual, BSD does the smart thing. You have given me an idea. The next time I'm questioned why I don't use GNU sed, I can say I'm paranoid about race conditions. Keep up the good work.
argv · 422 weeks and 5 days ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands



Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: