Other responses have latched on to perl -e and that's probably one good avenue to explore. Here's a real-world example for you. I recently downloaded a bunch of files from a Windows system and uploaded them to a FreeBSD system, only to find that the spaces in the file names had been encoded so they were now '%20'. I had to rename three directories, each containing six files.
After wimping out on the first two directories (this was time critical stuff and it was after midnight, a great time for making dumb mistakes, especially after a jam-packed 11 hour work day) I wrote
perl -e 'while(<*.mp3>){$orig=$_;s/%20/ /;print"$orig -> $_\n";}'
That seemed OK, but then I realized I'd have to escape the space in the middle of the name, so finally I wrote
perl -e 'while(<*.mp3>){$orig=$_;s/%20/\\ /;print`mv -v $orig $_`;}'
I ran it, and the verbose option confirmed that I'd done it correctly.
So what did that require that I wouldn't have had a clue how to do as a beginner?
- Remembering to use ' to quote a one liner so that I can write print statements with " so I can use variable interpolation;
- Making use of the glob operator to get all of the file names using a particular mask;
- (A Unix/Linux one: the -v switch makes a command 'verbose', useful when you want feedback about what it did);
- Remembering to save the original name when I want to have both the original and the new name;
- (A programming thing: Carefully review your code, thinking about exactly what it's going to do when it runs)
Well, that's not exactly an earth-shatteringly important list, but that's what I can squeeze out of that lemon.
Alex / talexb / Toronto
Life is short: get busy!
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.