|
Guides All Guides and HOWTO's. |
|
Thread Tools | Display Modes |
|
||||
I found J65nkos way of doing it interesting, but rather involved from my point of view; and several other posts also were interested. Being rather lazy, if I wanted to generate a string of mv commands to rename a group of files based on a simple pattern search & replace, at a unix shell, I would do it with a simple for loop and sed:
Code:
for FN in `ls *-*`; do mv "$FN" `echo "$FN" | sed 's/-/_/g'` done It is not an efficient way of doing it, but it is fast enough when the number of files is not huge; if they were, I would probably have fun in C (or assembly if it was truly a huge set of files...). Without the limitation of using mv, I would probably use this if not in a bourne shell: Code:
Terry@vectra$ perl -e 'map { $on=$_; s/-/_/; rename($on, $_) or warn $!; } <*>;'
__________________
My Journal Thou shalt check the array bounds of all strings (indeed, all arrays), for surely where thou typest ``foo'' someone someday shall type ``supercalifragilisticexpialidocious''. |
|
|||
Terry, that would change 'latest-pkg-erlangen' into 'latest_pkg_erlangen" which isn't what I needed. I only wanted the first '-' to be changed into an underscore, and not all of them
Another approach is to use a numerical flag after the sed search and replacement. From sed(1) Code:
[2addr]s/re/replacement/flags [snip] Th value of flags in the substitute function is zero or more of the following: 0 ... 9 Make the substitution only for the N'th occur- rence of the regular expression in the pattern space. Code:
$ echo 12345678 | sed -e 's/./#/4' 123#5678 $ echo 12345678 | sed -e 's/../#/2' 12#5678 The regular expression from the last example is '..', or a group of two whatever characters. So here the second pair of numbers '34' is replaced by a '#'. A single filename like 'latest-pkg-plig' can be converted rather easily into a 'mv latest-pkg-plig latest-pkg-plig'. To turn this intermediate result into a meaningful mv command, we only have to change the second occurrence of '-pkg' into '_pkg'. The first transformation and the explanation: Code:
$ echo latest-pkg-plig | sed -e 's/^.*$/mv & & /' mv latest-pkg-plig latest-pkg-plig # ---------------------------------------------------- s : search and replace / : delimiter marking start of search ^ : beginning of line . : whatever character * : zero or more of the preceding atom $ : end of line / : end of search pattern, start of replacement mv : a 'm', 'v', and a blank & : the matched searched pattern : a space & : the matched search pattern : a space / : end of replacement Code:
echo mv latest-pkg-plig latest-pkg-plig | sed -e 's/-pkg/_pkg/2' mv latest-pkg-plig latest_pkg-plig # ------------------------------- s : search and replace / : delimiter marking start of search -pkg : a sequence of '-', 'p', 'k', and 'g' / : end of search pattern, start of replacement _pkg : a '-', 'p', 'k' and a 'g' / : end of replacement 2 : only act on second occurence Code:
sed -e 's/^.*$/mv & & /' -e 's/-pkg/_pkg/2' tmp mv NOW/latest-pkg NOW/latest_pkg mv NOW/latest-pkg-erlangen NOW/latest_pkg-erlangen mv NOW/latest-pkg-plig NOW/latest_pkg-plig Code:
$ ls Snap2008-07-12_23.47_UTC/*-pkg* | sed -e 's/^.*$/mv & & /' -e 's/-pkg/_pkg/2' mv Snap2008-07-12_23.47_UTC/latest-pkg Snap2008-07-12_23.47_UTC/latest_pkg mv Snap2008-07-12_23.47_UTC/latest-pkg-arctic Snap2008-07-12_23.47_UTC/latest_pkg-arctic mv Snap2008-07-12_23.47_UTC/latest-pkg-calyx Snap2008-07-12_23.47_UTC/latest_pkg-calyx mv Snap2008-07-12_23.47_UTC/latest-pkg-plig Snap2008-07-12_23.47_UTC/latest_pkg-plig
__________________
You don't need to be a genius to debug a pf.conf firewall ruleset, you just need the guts to run tcpdump |
|
||||
Sorry, then I'll drop the 'g' next time ;-)
__________________
My Journal Thou shalt check the array bounds of all strings (indeed, all arrays), for surely where thou typest ``foo'' someone someday shall type ``supercalifragilisticexpialidocious''. |
|
|||
Hi,
How about using find to rename files? I've used this command to rename all .foo files to .bar Code:
$ ls 1.foo 2.foo 3.foo $ find . -type f -name "*.foo" -exec echo mv {} \`basename {} .foo\`.bar \; mv ./1.foo `basename ./1.foo .foo`.bar mv ./2.foo `basename ./2.foo .foo`.bar mv ./3.foo `basename ./3.foo .foo`.bar Code:
$ find . -type f -name "*.foo" -exec echo mv {} \`basename {} .foo\`.bar \; | sh $ ls 1.bar 2.bar 3.bar
__________________
"I never think of the future. It comes soon enough." - A.E Useful links: FreeBSD Handbook | FreeBSD Developer's Handbook | The Porter's Handbook | PF User's Guide | unix-heaven.org |
|
||||
I just discovered that on the Linux system at work here has the "rename" command.
According to the manpage it's part of the "util-linux-ng" package. Code:
RENAME(1) Linux Programmer’s Manual RENAME(1) NAME rename - Rename files SYNOPSIS rename from to file... rename -V DESCRIPTION rename will rename the specified files by replacing the first occurrence of from in their name by to. -V, --version Display version information and exit. For example, given the files foo1, ..., foo9, foo10, ..., foo278, the commands rename foo foo0 foo? rename foo foo0 foo?? will turn them into foo001, ..., foo009, foo010, ..., foo278. And rename .htm .html *.htm will fix the extension of your html files. SEE ALSO mmv(1), mv(1) AVAILABILITY The rename command is part of the util-linux-ng package and is available from ftp://ftp.kernel.org/pub/linux/utils/util-linux- ng/. 1 January 2000 RENAME(1) For example to replace all underscores with a plus: $ rename '_' '+' * One page I found on the internet (Which is about something different entirly and just mentions this command) used a regular expression, but that doesn't seem to work for me. Didn't research it.
__________________
UNIX was not designed to stop you from doing stupid things, because that would also stop you from doing clever things. |
|
||||
That rename command also comes with Slackware, and can be pretty handy. I missed having it on NetBSD, so I wrote a bash script to do the similar thing. It's attached below for anyone who is very bored. It seems to work, but hasn't been heavily used, no warranty. Probably has bash-isms.
It uses sed, but only in the most mundane way. |
Tags |
bre, regular expressions, vils |
|
|
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 |