Ithaca Free Software GNU Linux

A Central New York Free Software User Group
It is currently Thu Jul 29, 2010 7:09 am

All times are UTC - 4 hours




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: Mitch's list of really incredibly useful commands...
PostPosted: Mon Mar 12, 2007 3:45 pm 
Offline
User avatar

Joined: Tue Sep 05, 2006 12:57 pm
Posts: 159
Location: Freeville, NY
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.


Last edited by mitch on Tue Jun 26, 2007 9:32 am, edited 1 time in total.

Top
 Profile  
 
 Post subject: Recursively rename files using regular expressions
PostPosted: Thu Apr 26, 2007 10:30 am 
Offline
User avatar

Joined: Tue Sep 05, 2006 12:57 pm
Posts: 159
Location: Freeville, NY
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



Top
 Profile  
 
 Post subject: Get a list of all installed packages in Debian
PostPosted: Sun Apr 29, 2007 1:16 pm 
Offline
User avatar

Joined: Tue Sep 05, 2006 12:57 pm
Posts: 159
Location: Freeville, NY
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...


Top
 Profile  
 
 Post subject: Using Vim to replace PHP short tags with standard ones
PostPosted: Fri Jul 13, 2007 10:56 am 
Offline
User avatar

Joined: Tue Sep 05, 2006 12:57 pm
Posts: 159
Location: Freeville, NY
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 _ " ( ) [ ] $ ,".


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 4 hours


Who is online

Users browsing this forum: MSN [Bot] and 0 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group