mv a 2 digit to name....please help
So lets say I have in $FOLDER:
13432_32_12.nii 12341_35_12.nii 34523_45_12.nii . . . I want to replace (in this example) "_12.nii" with _MPRAGE.nii. Though in $FOLDER I have other files with _11.nii _5.nii , etc... :/ I am working with hundreds of them.... ######################### So the output should be: 13432_32_MPRAGE.nii 12341_35_MPRAGE.nii 34523_45_MPRAGE.nii . . . #The other files with other suffixes _10 _5 etc...should not do anything Here is what I have in mind but I am not good with regular expression so could you please help me? for FILE in $FOLDER; do if <some_regular that brings the "12> -eq "_12" ; then mv $FILE ${FILE/"_12.nii"/"MPRAGE.nii" fi done |
So is all you are wanting to do is replace the _12 with _MPRAGE? If so that would be very simple. Let us know because I think I may be missing something that you are trying to do here.
|
Which distro are you using? I ask because different distros use different types of the rename command.
For example, on Slackware you simply could do this: Code:
rename _12. _MPRAGE. *.nii |
An easy way from a GUI, if you have the ThunarBulkRename utility, is to do a Search and Replace on the Name only.
|
This would do what you want...
Code:
find . -type f -name '*_12.nii' | while read line;do mv "${line}" "${line%_12.nii}_MPRAGE.nii";done Code:
13432_32_12.nii SAM |
Nobody thought about the overwrite, excellent point sag47! I was waiting for the OP to repost with some more info before I jumped into giving code, some people are just copy/paste crazy and I have helped people break systems before with commands provided based upon incomplete or wrong info.
If you are going to use the mv command you can use the -i option so you are prompted in the event there would be any file name conflicts. |
Nope, just replace the _12.nii files and dont do anything with the other suffixes or replaces let say teh _5.nii with DTI.nii ... I think it look pretty simple with the rename command...the problem is if I have...
13432_12_12.nii 12341_35_12.nii 34523_45_12.nii . . . Wanting... ######################### 13432_12_MPRAGE.nii 12341_35_MPRAGE.nii 34523_45_MPRAGE.nii instead of : 13432_MPRAGE_MPRAGE.nii 12341_35_MPRAGE.nii 34523_45_MPRAGE.nii |
There should be no problem if you use the program like I described. it will not replace any occurrence of 12, it will only replace occurrences of _12. (including the dot at the end).
|
Both TobiSGD and my examples work. If you want to learn more about my example then look at the bash man page ("man bash" in terminal) and search for the section parameter expansion. Also, read the rename man page ("man rename") to understand TobSGD's command better.
|
All times are GMT -5. The time now is 08:52 AM. |