MiniScripts

Shell one-liners

Replace text in a list of files (NOTE: OVERWRITES ORIGINAL FILES):

  • Note that this will replace the original files, and mistakes in your command can cause them to be completely destroyed.
  • perl -i -p -e 's/ORIGINAL_TEXT/REPLACEMENT_TEXT/g;' FILE_OR_FILES_TO_OVERWRITE
  • Above: the "-i" option causes perl to overwrite the original file (it means modify "in place"). You may want to take it out and test it first, because this is NOT reversible and it's easy to ruin a lot of files this way.

Remove CVS locks from a repository that got messed up:

  • find /YOUR/DIRECTORY/PATH/TO/CVS   -name  "#cvs.lock*"  -print  |  xargs rm -rfd
  • Sometimes CVS will get messed up in the middle of an operation and will exit uncleanly, leaving lots of "lock" files behind. Then you won't be able to do anything in CVS until those are gone.
  • If there are more files that CVS complains about, then you can replace "#cvs.lock" with the names of the other files that CVS is telling you about.
  • Don't mess this up when using wildcards, because if you make it too general it will recursively delete everything in the directory! And since this is the master repository we are talking about, there will be no recourse! (Unless the master repository is on the backup.)
  • Moral of story: fancy rm commands are incredibly dangerous.

Add line numbers to the left side of a file:

  • cat YOURFILE | sed = | sed 'N;s/\n/\t/' > OUTPUT_FILE
  • If you want the line number delimited with something other than a tab, you can replace the \t with something else (perhaps a : or space).

-- Main.alexgw - 25 Jun 2007