Forums

Skip to content

Advanced search
  • Quick links
    • Unanswered topics
    • Active topics
    • Search
  • FAQ
  • Login
  • Register
  • Board index Assistance Portage & Programming
  • Search

sed nightmare

Problems with emerge or ebuilds? Have a basic programming question about C, PHP, Perl, BASH or something else?
Post Reply
Advanced search
10 posts • Page 1 of 1
Author
Message
drakonite
l33t
l33t
User avatar
Posts: 768
Joined: Sat Nov 02, 2002 7:24 am
Location: Lincoln, NE
Contact:
Contact drakonite
Website

sed nightmare

  • Quote

Post by drakonite » Thu Oct 02, 2003 10:11 am

I'm trying to write a "simple" line to rename a bunch of files from things like "foo [T+meh] (J).bar" or "ick [o37] (JU).ack" into nice names like "foo.bar" and "ick.ack"

I've gotten this so far :

Code: Select all

for i in *;do mv \'$i\' \'`echo $i|sed -e "s/'/\'/g" -e 's/ \[[A-Za-z0-9\+\!]*\]//g' -e 's/ ([J U E])//g'`\';done
If I replace mv with echo what it prints out looks like it should be right.. but when running it with mv it complains "mv: when moving multiple files, last argument must be a directory" for all the files...

Can anyone help me sort out this sed nightmare?
Shoot Pixels Not People

My GPG/PGP Public key
Top
TGL
Bodhisattva
Bodhisattva
Posts: 1978
Joined: Sun Jun 02, 2002 12:13 pm
Location: Rennes, France

  • Quote

Post by TGL » Thu Oct 02, 2003 12:39 pm

The nightmare is probably not in the sed command, but in your filename quoting. I would have used:

Code: Select all

... ; do mv "$i" "`...`" ; ...
That said, I've not checked your sed command, but according to your examples, all you have to do is to remove in the names everything from the first space (included) to the dot (not included). I would do it that way:

Code: Select all

for i in * ; do mv "$i" "`echo $i | sed -e 's:\ .*\.:\.:'`" ; done
Or are there some other files with space in their name that you don't want to change? In that case, you can try something like this:

Code: Select all

for i in * ; do (echo $i | grep "\[[A-Za-z0-9\+\!]*\]\ ([JUE]*)") && mv "$i" "`echo $i | sed -e 's:\ .*\.:\.:'`" ; done
(which will also give you feedback about what files are being renamed)
Top
fyerk
Apprentice
Apprentice
Posts: 212
Joined: Tue Sep 17, 2002 2:06 pm
Location: Atlanta, GA
Contact:
Contact fyerk
Website

  • Quote

Post by fyerk » Thu Oct 02, 2003 2:19 pm

Is it a requirement that you use sed? It seems like this would be much easier as a combination of find and a Perl one-liner.

This should do what you need...

Code: Select all

find . -maxdepth 1 -type f | perl -ple '$a=$_;s/([a-z]+)\s\[[^\]]+\]\([^\)]+\)/\1/;rename $a, $_'
Explanation can follow in another post if you're interested...
-David
Top
TGL
Bodhisattva
Bodhisattva
Posts: 1978
Joined: Sun Jun 02, 2002 12:13 pm
Location: Rennes, France

  • Quote

Post by TGL » Thu Oct 02, 2003 2:35 pm

fyerk wrote:It seems like this would be much easier as a combination of find and a Perl one-liner.
I tend to think that the right tool is the one you know, but it's somehow offtopic :D
Top
drakonite
l33t
l33t
User avatar
Posts: 768
Joined: Sat Nov 02, 2002 7:24 am
Location: Lincoln, NE
Contact:
Contact drakonite
Website

  • Quote

Post by drakonite » Thu Oct 02, 2003 10:18 pm

TGL wrote:
fyerk wrote:It seems like this would be much easier as a combination of find and a Perl one-liner.
I tend to think that the right tool is the one you know, but it's somehow offtopic :D
Yes, I have to agree ;) I don't know perl (or don't remember anything useful atleast ;) ) so my very trivial sed skills were what I chose.

Thanks for the help TGL. Changing how I was quoting did the trick. I've always been in the habit of using ' instead of " when typing filesnames.. hrmm, guess I should switch ;)

Now I just need to make it check if it should overwrite files or not and I'll be set ;) Shouldn't take me too long...
Shoot Pixels Not People

My GPG/PGP Public key
Top
pjp
Administrator
Administrator
User avatar
Posts: 20668
Joined: Tue Apr 16, 2002 10:35 pm

  • Quote

Post by pjp » Thu Oct 02, 2003 10:28 pm

Moved from Other Things Gentoo.
Quis separabit? Quo animo?
Top
funkmankey
Guru
Guru
User avatar
Posts: 304
Joined: Thu Mar 06, 2003 4:06 am
Location: CH
Contact:
Contact funkmankey
Website

  • Quote

Post by funkmankey » Thu Oct 02, 2003 10:32 pm

I chose the lazier route and installed krename. yes, it requires kdelibs and yes, kde is pure evil, but I'd already had to install evil before for some reason or other (I forget why now, though), so it wasn't such a big deal after all.

yes, I have a pretty decent grasp of regexes. but it always seems to turn out wrong, with the salt and the pain and the burning and the lost filesystems...
I've got the brain, I'm insane, you can't stop the power
Top
TGL
Bodhisattva
Bodhisattva
Posts: 1978
Joined: Sun Jun 02, 2002 12:13 pm
Location: Rennes, France

  • Quote

Post by TGL » Thu Oct 02, 2003 10:49 pm

funkmankey wrote:I chose the lazier route and installed krename.
For command line, you also have "ren" (emerge sys-apps/ren) which is very good for this kind of task. Here, it would have something like this:

Code: Select all

# ren "* [*] (*).*" "#1.#4"
Depending on some options, it will or not be interactive. But it so much funnier with bash and sed (or perl ;)).
Top
drakonite
l33t
l33t
User avatar
Posts: 768
Joined: Sat Nov 02, 2002 7:24 am
Location: Lincoln, NE
Contact:
Contact drakonite
Website

  • Quote

Post by drakonite » Thu Oct 02, 2003 10:55 pm

so NOW you break in with the ren and krename programs... lol
Shoot Pixels Not People

My GPG/PGP Public key
Top
TGL
Bodhisattva
Bodhisattva
Posts: 1978
Joined: Sun Jun 02, 2002 12:13 pm
Location: Rennes, France

  • Quote

Post by TGL » Thu Oct 02, 2003 11:08 pm

Sincerely, I've only reminded "ren" while reading funkmankey's post about "krename". But it doesn't mean I would have talked you about it before you've fixed your sed version :D
Top
Post Reply

10 posts • Page 1 of 1

Return to “Portage & Programming”

Jump to
  • Assistance
  • ↳   News & Announcements
  • ↳   Frequently Asked Questions
  • ↳   Installing Gentoo
  • ↳   Multimedia
  • ↳   Desktop Environments
  • ↳   Networking & Security
  • ↳   Kernel & Hardware
  • ↳   Portage & Programming
  • ↳   Gamers & Players
  • ↳   Other Things Gentoo
  • ↳   Unsupported Software
  • Discussion & Documentation
  • ↳   Documentation, Tips & Tricks
  • ↳   Gentoo Chat
  • ↳   Gentoo Forums Feedback
  • ↳   Duplicate Threads
  • International Gentoo Users
  • ↳   中文 (Chinese)
  • ↳   Dutch
  • ↳   Finnish
  • ↳   French
  • ↳   Deutsches Forum (German)
  • ↳   Diskussionsforum
  • ↳   Deutsche Dokumentation
  • ↳   Greek
  • ↳   Forum italiano (Italian)
  • ↳   Forum di discussione italiano
  • ↳   Risorse italiane (documentazione e tools)
  • ↳   Polskie forum (Polish)
  • ↳   Instalacja i sprzęt
  • ↳   Polish OTW
  • ↳   Portuguese
  • ↳   Documentação, Ferramentas e Dicas
  • ↳   Russian
  • ↳   Scandinavian
  • ↳   Spanish
  • ↳   Other Languages
  • Architectures & Platforms
  • ↳   Gentoo on ARM
  • ↳   Gentoo on PPC
  • ↳   Gentoo on Sparc
  • ↳   Gentoo on Alternative Architectures
  • ↳   Gentoo on AMD64
  • ↳   Gentoo for Mac OS X (Portage for Mac OS X)
  • Board index
  • All times are UTC
  • Delete cookies

© 2001–2026 Gentoo Foundation, Inc.

Powered by phpBB® Forum Software © phpBB Limited

Privacy Policy

 

 

magic