Ithaca Free Software GNU Linux
http://ithacafreesoftware.org/forum/

Mitch's list of really incredibly useful commands...
http://ithacafreesoftware.org/forum/viewtopic.php?f=7&t=213
Page 1 of 1

Author:  mitch [ Mon Mar 12, 2007 3:45 pm ]
Post subject:  Mitch's list of really incredibly useful commands...

This thread is simply a list of commands that I use from time to time, which are really useful, but long enough that I won't remember them when I need them.

Please note: for the rename examples below to work, you must be using the Perl version of rename, such as the one included in Debian (and derivative) distributions. The Redhat Enterprise Linux version of rename is different and is almost useless.

Rename files to add leading zeros (in this case followed by a letter)
Code:
$rename -nv 's/^([1-9])([a-z])/0$1$2/' /mydir/*

note: remove the 'n' argument to actually change the filenames.

Pipe the results of a file grep into vim tabs!
Code:
$vim -p `grep -rl "mystring" /mydir/*`

Once the files are open in Vim's tabs, you can use the ':tabdo' command to perform operations on all of the open buffers! Priceless!

Find files containing phone numbers in a collection of php files with grep
Code:
grep -rl "[[:digit:]]\{3\}[ -][[:digit:]]\{3\}[ -]\?[[:digit:]]\{4\}" * | grep "php"


Find phone numbers with spaces or dashes in Vim and replace with dots
Code:
%s/\([0-9]\{3}\)[ -]\([0-9]\{3}\)[ -]\([0-9]\{4}\)/\1\.\2\.\3/gc


Extract e-mail addresses from a text file
Code:
grep -oi [0-9A-Z\_\.\-]*\@[0-9A-Z\_\.\-]*\.[A-Z]* infile > outfile.txt

Note that the hyphens will only be matched if they are specified last in the range for some odd reason.

Author:  mitch [ Thu Apr 26, 2007 10:30 am ]
Post subject:  Recursively rename files using regular expressions

Rename files recursively with regular expression
This works with the Debian 'rename' Perl script. I think the Redhat version of 'rename' is different, and probably won't work. It also won't work if you have spaces in your filenames.
Code:
#!/bin/bash

for file in `find -name "/path/to/my/music/library/*.ogg"`
do
    rename -v 's/_-_/-/g' $file
done


For example, here's the script I used to clean up the problem characters in the filenames in my music library:

Code:
#!/bin/bash

# uncomment each of the rename commands (one at a time) and run the script
# you may need to run each rename operation more than once if a dir name is changed in the process
# change the '-nv' arg to '-v' to do the actual rename

for file in `find -name "/path/to/my/music/library/*"`
do
    #rename -nv 's/\(//g' $file
    #rename -nv 's/\)//g' $file
    #rename -nv "s/'//g" $file
    #rename -nv "s/,//g" $file
    #rename -nv "s/\&/and/g" $file
    #rename -nv "s/\[//g" $file
    #rename -nv "s/\]//g" $file
    #rename -nv "s/_\*_/-/g" $file
    #rename -nv "s/_-_/-/g" $file
    #rename -nv "s/!//g" $file
    #rename -nv "s/\%2f/-/g" $file
    #rename -nv "s/\%2F/-/g" $file
    #rename -nv "s/\://g" $file
    #rename -nv "s/_w-_/-/g" $file
done


Author:  mitch [ Sun Apr 29, 2007 1:16 pm ]
Post subject:  Get a list of all installed packages in Debian

Here's a command that will list all installed packages on a Debian-based system:

Code:
dpkg --get-selections | grep -v deinstall | cut -f 1


This would be useful to pass into a script if you wanted to set up a new system and a simple clone wouldn't work...

Author:  mitch [ Fri Jul 13, 2007 10:56 am ]
Post subject:  Using Vim to replace PHP short tags with standard ones

Recently, I have begun to replace the use of PHP "short tags" (e.g. using "<?" and "<?=" in PHP code instead of the standard tags "<?php" and <?php echo ...").

To make this easier, I've developed the following regular expression substitution commands in VIM to automatically find and replace the PHP short tags with standard ones.

Note that since there are two ways that the PHP short tags are used in PHP code: the first to indicate a block of code, and the second to just echo a single variable (<?=$foo?>, that I use two separate substitutions. Each engineered so as to not make things harder for the other.

0. Find (recursively) all files in the current directory using PHP "short tags"
Code:
grep -rl "<?[^p]" *


1. Replace "<?" at the beginning of code blocks with "<?php."
Code:
%s/<?\([ \t\r\n]*[^a-zA-Z=]\)/<?php\1/gc


This substitution doesn't currently find all the opening short tags... Still working on it.

Notes on the expression:
"[ \t\r\n]*" matches any whitespace characters
"[^a-zA-Z=]" excludes any alpha characters or equal signs immediately following the "<?". This will avoid replacing properly written standard tags "<?php", and munging tags intended to be replaced by the "<?=$foo?> fixing substitution.

2. replace "<?=$foo?>" with "<?php echo $foo;?>."
Code:
%s/<?=\([0-9a-zA-Z_\'\"\(\)\[\]\$,]*\)?>/<?php echo \1;?>/gc

Notes on the expression: the strings we're trying to substitute can contain any of the following characters, so we have to include them in the expression: "0-9 a-z A-Z _ " ( ) [ ] $ ,".

Page 1 of 1 All times are UTC - 4 hours
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/