Author Topic: Renaming/editing a specific part of a file  (Read 918 times)

0 Members and 1 Guest are viewing this topic.

Offline pooky2483

  • Hero Member
  • *****
  • Posts: 1620
  • Karma: 0
  • Gender: Male
  • Slowly getting the hang of it.
    • View Profile
    • Get your FREE Ubuntu stickers here. I'm the UK address
    • Awards
Renaming/editing a specific part of a file
« on: October 20, 2013, 02:28:34 am »
I am trying to edit rather a lot of files in different directories and finding it such a bore doing it in PyRename.
Filenames such as 'Clear.and.Present.Danger.1994.1080p.BluRay.x264.YIFY.mp4'
I want to change it so that the year is in '()' but some of the filenames already have the year in brackets or theres a duplicate but in a different format such as '.avi' or '.mkv'

I have had a look elsewhere (naughty, I know ;) )and found this;
http://askubuntu.com/questions/22760/how-do-you-rename-files-ignoring-file-extension-in-terminal

and this line in particular...
Code: [Select]
for file in * ; do mv "$file" "${file//bad/good}" ; done

But I know there's going to be more involved and it may end up as a script to execute.
I've got 'Linux Pocket Guide' but it goes nowhere near the stuff I need to do, so I am going to hopefully soon get more books to help me.

Kubuntu 12.04LTS 64bit|KDE 4.13.2|QT 4.8.6|Linux 3.2.0-68-generic|M3A76-CM|BIOS 2101|AMD PhenomII X4 965 3400+|Realtek RTL8168C(P)|8111C(P) PCI-E Gigabit Ethernet NIC|NVIDIA 128MB GeForce6200 Turbocache|8.0GB Single-Channel DDR2|

Online Mark Greaves (PCNetSpec)

  • Administrator
  • Hero Member
  • *****
  • Posts: 13984
  • Karma: 347
  • Gender: Male
  • "-rw-rw-rw-" .. The Number Of The Beast
    • View Profile
    • PCNetSpec
    • Awards
Re: Renaming/editing a specific part of a file
« Reply #1 on: October 20, 2013, 03:13:23 am »
if you modify that to
Code: [Select]
for file in * ; do mv "$file" "${file//1994/(1994)}" ; done
that would work .. but only for the current directory .. not sub direcories

This will also do subdirectories:
Code: [Select]
find . -depth -name '*1994*' -execdir bash -c 'for file; do mv -i "$file" "${file//1994/(1994)}"; done' bash {} +

But the problem is, you'd have to do that (and modify it) for every year .. on top of that it would also change (1994) to ((1994))

There is no single line command for what you want to do .. you'd probably have to get a bit creative with a script.

Now I've said that, watch MP come along and prove me wrong with some weird sed command.

--
« Last Edit: October 20, 2013, 03:39:27 am by Mark Greaves (PCNetSpec) »
WARNING: You are logged into reality as 'root'

logging in as 'insane' is the only safe option.

Offline Mad Penguin

  • #Mad_Penguin_UK
  • Administrator
  • Hero Member
  • *****
  • Posts: 1320
  • Karma: 10017
  • Gender: Male
    • View Profile
    • Linux in the UK
    • Awards
Re: Renaming/editing a specific part of a file
« Reply #2 on: October 20, 2013, 11:20:21 am »
Ok, but difficult without a better selection of filenames, but how about this;
Code: [Select]
find . -exec awk '{ printf "mv %s ",%0;   gsub(".(19|20)...",".(&)."); gsub(".\\(.",".("); gsub(".\\).",")."); print }' {} \;
This will match all files containing .19??. and .20??.
It will then print "mv (original file name) (file name with brackets around the year).
So if you do this command and so "> out.sh" , you can then do "chmod a+x out.sh; ./out.sh" to execute the commands.
(you could stick this last but on the same line as the find, but this way you can look at out.sh and make sure it's going to do what you expect)

Example;
Code: [Select]
echo "111.222.333.2012.xxx" | awk  ....
mv 111.222.333.2012.xxx 111.222.333.(2012).xxx

Online Mark Greaves (PCNetSpec)

  • Administrator
  • Hero Member
  • *****
  • Posts: 13984
  • Karma: 347
  • Gender: Male
  • "-rw-rw-rw-" .. The Number Of The Beast
    • View Profile
    • PCNetSpec
    • Awards
Re: Renaming/editing a specific part of a file
« Reply #3 on: October 20, 2013, 02:41:20 pm »
There appears to be a syntax error after the "find" .. I get a ton of these:-

Code: [Select]
mark@AA1-Blue ~/test $ find . -exec awk '{ printf "mv %s ",%0;   gsub(".(19|20).
..",".(&)."); gsub(".\\(.",".("); gsub(".\\).",")."); print }' {} \;
awk: cmd. line:1: { printf "mv %s ",%0;   gsub(".(19|20)...",".(&)."); gsub(".\\
(.",".("); gsub(".\\).",")."); print }
awk: cmd. line:1:                   ^ syntax error

That's gobbledygook to me so understanding/fixing would be a problem  :-[

or am I just being thick here ???
WARNING: You are logged into reality as 'root'

logging in as 'insane' is the only safe option.

Offline Mad Penguin

  • #Mad_Penguin_UK
  • Administrator
  • Hero Member
  • *****
  • Posts: 1320
  • Karma: 10017
  • Gender: Male
    • View Profile
    • Linux in the UK
    • Awards
Re: Renaming/editing a specific part of a file
« Reply #4 on: October 20, 2013, 03:26:23 pm »
Well I can't make it "too" easy for you !! .. had to leave in the deliberate typo ...  ;)

It should be $0, not %0 ... :)

This is a better calling method ...

Code: [Select]
find . | awk '{ printf "mv %s ",$0;   gsub(".(19|20)...",".(&)."); gsub(".\\(.",".("); gsub(".\\).",")."); print }' 
« Last Edit: October 20, 2013, 03:35:13 pm by Mad Penguin »

Offline Mad Penguin

  • #Mad_Penguin_UK
  • Administrator
  • Hero Member
  • *****
  • Posts: 1320
  • Karma: 10017
  • Gender: Male
    • View Profile
    • Linux in the UK
    • Awards
Re: Renaming/editing a specific part of a file
« Reply #5 on: October 20, 2013, 03:32:49 pm »
Here's the explanation, working without the find (which repeats the command per file);

Code: [Select]
printf "mv %s", $0"
Print the original filename ($0) with a an "mv" command and no newline.

Quote
gsub(".(19|20)...",".(&).");

Replace any occurrence of "." followed by 19 or 20, followed by any two digits, followed by a dot, with the original match, surrounded by ".( )."
(the deliberate "bug" here is ".(19|20)..." should probably be "\\.(19|20)..\\." - someone can test this .. :)  )

Quote
gsub(".\\(.",".(");

Replace all ".(." with ".("

Quote
gsub(".\\).",").");

Replace all ".)." with ").".

Quote
print

Print what's left.
Simplez ..  ;)




Offline pooky2483

  • Hero Member
  • *****
  • Posts: 1620
  • Karma: 0
  • Gender: Male
  • Slowly getting the hang of it.
    • View Profile
    • Get your FREE Ubuntu stickers here. I'm the UK address
    • Awards
Re: Renaming/editing a specific part of a file
« Reply #6 on: October 28, 2013, 06:43:07 pm »
Thanks for the help there Mark & MP.
I've not got round to executing the command as I'm renaming the files, editing out other spurious rubbish first.
Also, just a thought, remembering back in '91-'92 while on the programming course. I'm sure I remember something about being able to differentiate between numbers and alphabet, so isn't there some sort of 'wildcard' for numbers, separate to alphabet. Can't we use that in some way?

Kubuntu 12.04LTS 64bit|KDE 4.13.2|QT 4.8.6|Linux 3.2.0-68-generic|M3A76-CM|BIOS 2101|AMD PhenomII X4 965 3400+|Realtek RTL8168C(P)|8111C(P) PCI-E Gigabit Ethernet NIC|NVIDIA 128MB GeForce6200 Turbocache|8.0GB Single-Channel DDR2|

Offline pooky2483

  • Hero Member
  • *****
  • Posts: 1620
  • Karma: 0
  • Gender: Male
  • Slowly getting the hang of it.
    • View Profile
    • Get your FREE Ubuntu stickers here. I'm the UK address
    • Awards
Re: Renaming/editing a specific part of a file
« Reply #7 on: November 18, 2013, 04:27:07 pm »
Just tested it on a couple of files and it produced this result with one of the files;
mv ./Monty Python's Life of Brian 1979 720p DTS multisub GER HUN HighCode.mkv ./Monty Python's Life of Brian.(1979)..(20p ).TS multisub GER HUN HighCode.mkv

Notice the 720p changed to (20p ) - Not the desired result.

Kubuntu 12.04LTS 64bit|KDE 4.13.2|QT 4.8.6|Linux 3.2.0-68-generic|M3A76-CM|BIOS 2101|AMD PhenomII X4 965 3400+|Realtek RTL8168C(P)|8111C(P) PCI-E Gigabit Ethernet NIC|NVIDIA 128MB GeForce6200 Turbocache|8.0GB Single-Channel DDR2|

Offline pooky2483

  • Hero Member
  • *****
  • Posts: 1620
  • Karma: 0
  • Gender: Male
  • Slowly getting the hang of it.
    • View Profile
    • Get your FREE Ubuntu stickers here. I'm the UK address
    • Awards
Re: Renaming/editing a specific part of a file
« Reply #8 on: November 18, 2013, 04:28:41 pm »
Also... Where did it put the new file????

Kubuntu 12.04LTS 64bit|KDE 4.13.2|QT 4.8.6|Linux 3.2.0-68-generic|M3A76-CM|BIOS 2101|AMD PhenomII X4 965 3400+|Realtek RTL8168C(P)|8111C(P) PCI-E Gigabit Ethernet NIC|NVIDIA 128MB GeForce6200 Turbocache|8.0GB Single-Channel DDR2|

 


SimplePortal 2.3.3 © 2008-2010, SimplePortal