Using tr to replace strings in a group of filenames
Hello,
I have hundreds of filenames of the form CEU_NA21322_NSP.CEL. I am trying to use tr to first systemactically remove the 'CEU_' prefix; then remove the '_NSP' string; and finally, replace the suffix with ',CEU.CEL'. So that in the end this filename will be NA21322,CEU.CEL For the first step this is what I am trying: mv *.CEL `echo *.CEL | tr -d [="CEU_"=]` tr: CEU_: equivalence class operand must be a single character mv: missing destination file operand after `CEU_NA3242_NSP.CEL' Try `mv --help' for more information. Can anyone help? |
Not saying you couldn't do it with tr with some effort but this is really a job for sed:
If you did something like: Code:
echo CEU_NA21322_NSP.CEL |sed -e s/^CEU_// -e s/_NSP/,CEU/ For a list of files you could do this with a "for" loop: Code:
for FILE in `ls CEU_*NSP.CEL` I'd recommend you copy a couple of files to a completely different directory and test the above on that new directory to be sure the results are what you expect. The above is provided "as is" and except for the original echo line has NOT been tested by me. Testing is very important. |
Quote:
Code:
for FILE in CEU_*NSP.CEL Code:
#!/usr/bin/perl -w my perl knowledge is quite limited, the above is probably twice as long/inefficient as necessary. It seems to work but test it first, as jlightner suggested. |
Thanks for the help guys. I am more comfortable with bash shell scripts, so I will stick to using that for now. If I write a script though, how can I pass the contents of 'ls' to that script as an argument?
Thanks for you help! |
You don't need to, and shouldn't, pass the output of ls. It's a bad idea to rely on ls's output since it's not portable, it varies according to environment variables, and it's changed over time.
If you just want to iterate over a list of files, you can pipe the output of find to the script, or just use shell globbing. If you need to know the date/size/whatever, it's better to use stat than the output of ls (ls uses stat anyway). You might want to write the script so you can use it both ways (piping filenames in or passing on the command line) E.g. Code:
#!/bin/sh Code:
Code:
recursion="" |
Quote:
for ff in 'CEU_*NSP.CEL'; do NEWFILE='echo $ff |sed -e s/^CEU_// -e s/_NSP/,CEU/'; mv $ff $NEWFILE; done I get the following error message when running it as a script: mv: invalid option -- e Try `mv --help' for more information. |
The code I suggested DOES work because I tested it before posting.
It appears you combined advice of two posters: In mine I had you do `ls CEU_*NSP.CEL` (back ticks included) to get the list of files. A later poster said to drop the ls. He also meant for you to drop the back ticks. The back ticks say "execute this command before the rest of the command line". He basically was saying you don't need to and shouldn't do the ls syntax I'd provided. I personally don't think his objections were that valid but also don't think he was wrong in saying it would work his way. You can do it the way I had it OR the way he had it. I haven't tested it his way but don't see any reason it wouldn't work. There is a question why it passed the "-e" to your mv command since it is in the sed syntax rather than the mv syntax. I wouldn't investigate that until you correct as noted above. If you still see the same error it may indicate you accidentally created a file named "-e". That occurs on occasion when you type something incorrectly. If you have such a file you can delete it by typing: rm ./"-e" in the directory where the file exists. P.S. When asking for help it might be better to say "I couldn't get it working" than to say "This code chunk you suggested does not work". It allows for the idea that the mistake was yours rather than that of the person that was attempting to help you and might encourage them to follow up. Not saying I can't a mistake but here the mistake was yours and in a different mood I might just have ignored your post or replied with a simple flame. |
Thanks alot for your help. Ive worked out the problems (I wasnt properly doing the command subsitution previously) and as you have suggested the following works:
for ff in CEU_*NSP.CEL do NEWFILE=$(echo $ff |sed -e s/^CEU_// -e s/_NSP/,CEU/) mv $ff $NEWFILE done |
All times are GMT -5. The time now is 05:00 PM. |