![]() |
|
Guides All Guides and HOWTO's. |
![]() |
|
Thread Tools | Display Modes |
|
|||
![]()
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 |
|
|||
![]()
Never heard of vils before. Using that utility simplifies things a lot because you don't have to generate the "mv <original_file>" part of the renaming commands.
How does vils handle spaces in file names?
__________________
You don't need to be a genius to debug a pf.conf firewall ruleset, you just need the guts to run tcpdump |
|
||||
![]()
Never tried that before, so let's test.
Code:
% vils 1 0001 last_pkg 2 0002 last_pkg-calyx 3 0003 latest_pkg-erlangen 4 0004 latest_pkg-esat 5 0005 latest_pkg-plig 6 0006 latest_pkg-stacken :%s/_/ / 1 0001 last pkg 2 0002 last pkg-calyx 3 0003 latest pkg-erlangen 4 0004 latest pkg-esat 5 0005 latest pkg-plig 6 0006 latest pkg-stacken :wq sverre % ls last pkg latest pkg-erlangen latest pkg-plig last pkg-calyx latest pkg-esat latest pkg-stacken sverre % Code:
vils 1 0001 last pkg 2 0002 last pkg-calyx 3 0003 latest pkg-erlangen 4 0004 latest pkg-esat 5 0005 latest pkg-plig 6 0006 latest pkg-stacken :%s/ /_/ 1 0001_last pkg 2 0002_last pkg-calyx 3 0003_latest pkg-erlangen 4 0004_latest pkg-esat 5 0005_latest pkg-plig 6 0006_latest pkg-stacken ![]() Code:
1 0001 last pkg 2 0002 last pkg-calyx 3 0003 latest pkg-erlangen 4 0004 latest pkg-esat 5 0005 latest pkg-plig 6 0006 latest pkg-stacken :%s/\([A-z]\) /\1_/ 1 0001 last_pkg 2 0002 last_pkg-calyx 3 0003 latest_pkg-erlangen 4 0004 latest_pkg-esat 5 0005 latest_pkg-plig 6 0006 latest_pkg-stacken :wq sverre % ls last_pkg latest_pkg-erlangen latest_pkg-plig last_pkg-calyx latest_pkg-esat latest_pkg-stacken sverre % ~ ![]() Of course, since we are actually editing the file names in vi, we could also replace each blank with an underscore manually, or by using the command: Code:
:%s/ /_/gc ![]() ![]() |
|
|||
![]()
You also can restore the blanks into underscores with:
Code:
:%s/ pkg/_pkg/ Code:
:%! pkg!_pkg! Code:
s!^\([^-]*\)-\(pkg.*\)!mv \1-\2 \1_\2! Code:
s#^\([^-]*\)-\(pkg.*\)#mv \1-\2 \1_\2#
__________________
You don't need to be a genius to debug a pf.conf firewall ruleset, you just need the guts to run tcpdump |
|
||||
![]() Quote:
Quote:
|
|
||||
![]()
There is also some bash tricks that should be able to do that in one step. Let's see if I can work it out.
Code:
robbak@robbak-PC:~/testdir$ touch file-one robbak@robbak-PC:~/testdir$ touch file-two robbak@robbak-PC:~/testdir$ touch file-three robbak@robbak-PC:~/testdir$ touch "with space-four" robbak@robbak-PC:~/testdir$ for filename in *; do mv "$filename" "${filename/-/_}"; done robbak@robbak-PC:~/testdir$ ls file_one file_three file_two with space_four robbak@robbak-PC:~/testdir$
__________________
The only dumb question is a question not asked. The only dumb answer is an answer not given. |
|
||||
![]()
Nice little script, robbak!
What if I want to change all "-" to "_" in the filenames? Is there a command for that too in bash? Quote:
Code:
$ for filename in * > do > mv "$filename" "${filename/-/_}" > done ${filename/...}: Bad substitution |
|
||||
![]() Quote:
Code:
robbak@robbak-PC:~/testdir$ ls file-one file-three file-two with-space-five-mess with space-four robbak@robbak-PC:~/testdir$ for filename in *; do mv "$filename" "${filename//-/_}"; done robbak@robbak-PC:~/testdir$ ls file_one file_three file_two with_space_five_mess with space_four robbak@robbak-PC:~/testdir$
__________________
The only dumb question is a question not asked. The only dumb answer is an answer not given. |
|
||||
![]()
Great! So now we have three good ways of doing this renaming:
Let's go out in the world and look for files to rename! ![]() I will start renaming the files in /bin/ and /usr/bin. ![]() ![]() Just kidding! Last edited by sverreh; 13th December 2008 at 03:39 PM. |
|
|||
![]()
The perl version is less incomprehensible as the sed one.
Code:
$ perl -n -e 's/^([^-]*)-(pkg.*)/mv $1-$2 $1_$2/; print;' 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 The differences Code:
sed perl \( ( \) ) \1 $1 \2 $2 ![]()
__________________
You don't need to be a genius to debug a pf.conf firewall ruleset, you just need the guts to run tcpdump |
|
||||
![]() Quote:
![]() Well done! So when will we have versions in C, C++, C#, Python, HTML, PHP, Cobol, Fortran, Algol, Simula, Basic and Haskell? This project is taking off! ![]() |
|
||||
![]()
You can't rename a file in [pure] HTML, and it lacks Regex.
__________________
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''. |
|
|||
![]()
You can generate those 'mv' commands with XSLT though.
Source XML file "files,xml": Code:
<?xml version="1.0" ?> <?xml-stylesheet href="rename.xsl" type="text/xsl" ?> <collection> <file>latest-pkg</file> <file>latest-pkg-calyx</file> <file>latest-pkg-erlangen</file> <file>latest-pkg-esat</file> <file>latest-pkg-plig</file> <file>latest-pkg-stacken</file> </collection> Code:
<?xml version="1.0" encoding='UTF-8' ?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method='html' omit-xml-declaration='no' indent='yes' standalone='yes' doctype-public='-//W3C//DTD XHTML 1.0 Strict//EN' doctype-system='http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd' /> <xsl:template match="/collection"> <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en" > <head> <title> Renaming a file with XSL </title> </head> <body> <xsl:call-template name='show_original' /> <xsl:call-template name='show_rename_cmd' /> </body> </html> </xsl:template> <xsl:template name="show_original"> <h1>Original file names</h1> <ul> <xsl:for-each select='file' > <li><xsl:value-of select="." /> </li> </xsl:for-each> </ul> </xsl:template> <xsl:template name="show_rename_cmd"> <h1>Rename command</h1> <ul> <xsl:for-each select='file' > <li> <xsl:value-of select='concat("mv ", current(), " ")' /> <xsl:value-of select="substring-before( current(),'-')" /> <xsl:text>_</xsl:text> <xsl:value-of select="substring-after( current(),'-')" /> </li> </xsl:for-each> </ul> </xsl:template> </xsl:stylesheet> Code:
Original file names * latest-pkg * latest-pkg-calyx * latest-pkg-erlangen * latest-pkg-esat * latest-pkg-plig * latest-pkg-stacken Rename command * mv latest-pkg latest_pkg * mv latest-pkg-calyx latest_pkg-calyx * mv latest-pkg-erlangen latest_pkg-erlangen * mv latest-pkg-esat latest_pkg-esat * mv latest-pkg-plig latest_pkg-plig * mv latest-pkg-stacken latest_pkg-stacken Feel free to view the source code. In firefox: View -> Page Source, or just press CNTRL+U
__________________
You don't need to be a genius to debug a pf.conf firewall ruleset, you just need the guts to run tcpdump Last edited by J65nko; 14th December 2008 at 06:37 PM. Reason: Added download |
|
|||
![]()
Using OpenBSD make:
Code:
$ cat Makefile FILES = latest-pkg latest-pkg-calyx latest-pkg-erlangen \ latest-pkg-esat latest-pkg-plig latest-pkg-stacken main: .for THIS in ${FILES} @echo mv ${THIS} ${THIS:S/-pkg/_pkg/g} .endfor $ make mv latest-pkg latest_pkg mv latest-pkg-calyx latest_pkg-calyx mv latest-pkg-erlangen latest_pkg-erlangen mv latest-pkg-esat latest_pkg-esat mv latest-pkg-plig latest_pkg-plig mv latest-pkg-stacken latest_pkg-stacken
__________________
You don't need to be a genius to debug a pf.conf firewall ruleset, you just need the guts to run tcpdump |
|
||||
![]()
FreeBSD supports ${var:S/} too, and some other variants, all documented in make(1)
__________________
UNIX was not designed to stop you from doing stupid things, because that would also stop you from doing clever things. |
|
||||
![]()
Sorry to kick-in late, great guide j65nko! But was there a need for using regexp? Could have a simple sed 's/-/_/' file do the job? I see the pattern is repeating and you needed to change only the first occurrence of "-" in string (note there is no g at the end).
Also noone offered an awk solution to the problem: Code:
awk '{ sub(/-/,"_"); print }' file
__________________
The best way to learn UNIX is to play with it, and the harder you play, the more you learn. If you play hard enough, you'll break something for sure, and having to fix a badly broken system is arguably the fastest way of all to learn. -Michael Lucas, AbsoluteBSD |
|
|||
![]()
The input was a list of files, the output should be mv commands to rename the files. The sed solution is a little bit complicated, because there is no easy way to save the original file name.
Your one-liner does not create these 'mv' commands files, as was the requirement ![]() This modification of your script does produce them: Code:
{ orig = $1 sub(/-/,"_") printf("mv %s %s\n", orig, $1) ; } Code:
$ awk -f ren_awk 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
__________________
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 | |
|
|
![]() |
||||
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 |