LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-11-2013, 06:04 PM   #1
Drigo
Member
 
Registered: Jun 2009
Posts: 111

Rep: Reputation: 17
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
 
Old 02-11-2013, 06:17 PM   #2
Kustom42
Senior Member
 
Registered: Mar 2012
Distribution: Red Hat
Posts: 1,604

Rep: Reputation: 415Reputation: 415Reputation: 415Reputation: 415Reputation: 415
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.
 
Old 02-11-2013, 06:24 PM   #3
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,131
Blog Entries: 2

Rep: Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833
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
On other distributions other syntax may be used, so have a look at the man-page for your rename command.
 
2 members found this post helpful.
Old 02-11-2013, 06:28 PM   #4
allend
Senior Member
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware-current
Posts: 4,466

Rep: Reputation: 1370Reputation: 1370Reputation: 1370Reputation: 1370Reputation: 1370Reputation: 1370Reputation: 1370Reputation: 1370Reputation: 1370Reputation: 1370
An easy way from a GUI, if you have the ThunarBulkRename utility, is to do a Search and Replace on the Name only.
 
Old 02-11-2013, 06:38 PM   #5
sag47
Senior Member
 
Registered: Sep 2009
Location: Orange County, CA
Distribution: Kubuntu x64, Raspbian, CentOS
Posts: 1,845
Blog Entries: 36

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
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
However I'm confused about one thing. You say there are other files ending with _5.nii and _11.nii. Do you want those to be renamed as well? If you do want those to be renamed then how would you handle the case of the following two files?

Code:
13432_32_12.nii
13432_32_11.nii
If you renamed both of them to end with _MPRAGE.nii then one of the files would be overwritten by the other.

SAM

Last edited by sag47; 02-11-2013 at 06:40 PM.
 
1 members found this post helpful.
Old 02-11-2013, 06:49 PM   #6
Kustom42
Senior Member
 
Registered: Mar 2012
Distribution: Red Hat
Posts: 1,604

Rep: Reputation: 415Reputation: 415Reputation: 415Reputation: 415Reputation: 415
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.
 
Old 02-12-2013, 12:17 AM   #7
Drigo
Member
 
Registered: Jun 2009
Posts: 111

Original Poster
Rep: Reputation: 17
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
 
Old 02-12-2013, 12:26 AM   #8
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,131
Blog Entries: 2

Rep: Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833
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).

Last edited by TobiSGD; 02-12-2013 at 12:27 AM.
 
Old 02-12-2013, 12:41 AM   #9
sag47
Senior Member
 
Registered: Sep 2009
Location: Orange County, CA
Distribution: Kubuntu x64, Raspbian, CentOS
Posts: 1,845
Blog Entries: 36

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
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.

Last edited by sag47; 02-12-2013 at 12:45 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Last digit minus ONE... NetRock Programming 5 04-06-2011 01:39 PM
[SOLVED] Get first digit of each line X.Cyclop Programming 3 11-24-2010 09:19 PM
Substitue single-digit, two-digit, and 3-digit numbers with text using sed dmason165 Programming 13 08-07-2009 11:38 AM
C - how to check whether it's a digit sycamorex Programming 10 02-28-2008 06:37 PM
54 digit numbers Scratchit General 3 06-23-2004 10:24 AM


All times are GMT -5. The time now is 01:06 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration