|
Guides All Guides and HOWTO's. |
|
Thread Tools | Display Modes |
|
|||
Regular expressions: renaming files with 'sed'
I hade a bunch of directories with files like this:
Code:
$ ls NOW/latest-pkg* NOW/latest-pkg NOW/latest-pkg-erlangen NOW/latest-pkg-plig NOW/latest-pkg-calyx NOW/latest-pkg-esat NOW/latest-pkg-stacken The plan was to save the file names in a temporay file and use sed to create sh 'mv' commands to rename the files. First save the file names in a file called 'tmp'. Code:
$ ls NOW/latest-pkg* >tmp $ cat tmp NOW/latest-pkg NOW/latest-pkg-calyx NOW/latest-pkg-erlangen NOW/latest-pkg-esat NOW/latest-pkg-plig NOW/latest-pkg-stacken Code:
mv NOW/latest-pkg NOW/latest_pkg
Code:
$ sed -e 's/^\([^-]*\)-\(pkg.*\)/mv \1-\2 \1_\2/' tmp mv NOW/latest-pkg NOW/latest_pkg mv NOW/latest-pkg-calyx NOW/latest_pkg-calyx mv NOW/latest-pkg-erlangen NOW/latest_pkg-erlangen mv NOW/latest-pkg-esat NOW/latest_pkg-esat mv NOW/latest-pkg-plig NOW/latest_pkg-plig mv NOW/latest-pkg-stacken NOW/latest_pkg-stacken We now have two options to execute this:
I went for the last option. The result: Code:
$ ls -l NOW/latest_* -rw-r--r-- 1 j65nko j65nko 253269 Dec 12 02:41 NOW/latest_pkg -rw-r--r-- 1 j65nko j65nko 293597 Dec 12 02:41 NOW/latest_pkg-calyx -rw-r--r-- 1 j65nko j65nko 253269 Dec 12 02:41 NOW/latest_pkg-erlangen -rw-r--r-- 1 j65nko j65nko 253269 Dec 12 02:42 NOW/latest_pkg-esat -rw-r--r-- 1 j65nko j65nko 252528 Dec 12 02:41 NOW/latest_pkg-plig -rw-r--r-- 1 j65nko j65nko 253269 Dec 12 02:41 NOW/latest_pkg-stacken Code:
s/^\([^-]*\)-\(pkg.*\)/mv \1-\2 \1_\2/ Code:
s/^\([^-]*\)-\(pkg.*\)/ s : search / : delimiter to mark start of search pattern ^ : beginning of line \( : start saving text for replay in containter \1 [^-] : whatever character as long it is not a '-' * : zero or more of the preceding \) : stop saving in container \1 we now have saved or stored the text 'NOW/latest' - : a '-', which we didn't save because we need to replace it with a underscore '_' \( : start saving text for replay in container \1 pkg : a 'p', followed by a 'k', followed by a 'g' . : followed by whatever character * : zero or more of instances of the preceding atom '.' \) : stop saving in container \2 Code:
/ mv \1-\2 \1_\2/ / : end of seach pattern, start of replacement mv : a 'm' and a 'v' followed by a space : now we reconstruct our original file name: \1 : replay or the text 'NOW/latest' from container \1 - : a '-' \2 : the text from container \2 : a space to separate the original name from the new one \1 : fetch text 'latest' from container \1 _ : the underscore we want \2 : the remainder of the file name from container \2
__________________
You don't need to be a genius to debug a pf.conf firewall ruleset, you just need the guts to run tcpdump |
Tags |
bre, regular expressions, vils |
Thread Tools | |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Cleaning Portsnap files in /var/db/portsnap/files | bram85 | FreeBSD Ports and Packages | 2 | 5th October 2009 09:54 AM |
PHP regular expression help | cajunman4life | Programming | 2 | 16th August 2008 05:17 PM |
How to sync files over ftp | graudeejs | FreeBSD General | 4 | 4th August 2008 10:18 PM |
Mount filesystem with a regular user | ivanatora | FreeBSD General | 15 | 30th July 2008 08:51 AM |
are you an former bsdforum regular? | ephemera | Off-Topic | 18 | 28th July 2008 12:57 PM |