LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 06-08-2010, 05:57 PM   #1
xmrkite
Member
 
Registered: Oct 2006
Location: California, USA
Distribution: Mint 16, Lubuntu 14.04, Mythbuntu 14.04, Kubuntu 13.10, Xubuntu 10.04
Posts: 554

Rep: Reputation: 30
How do you remove Parenthesis from filenames using sed


Hello,

I have filenames like such: abc (e).doc

And I want to rename them to abc.doc

I have a directory full of files names like this.

How can i do this using the sed command?

I have looked online for about 2-3 hours now and am frustrated that I can't find an answer.

Any help will be most appreciated

-Thanks
 
Old 06-08-2010, 06:18 PM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,617

Rep: Reputation: 7963Reputation: 7963Reputation: 7963Reputation: 7963Reputation: 7963Reputation: 7963Reputation: 7963Reputation: 7963Reputation: 7963Reputation: 7963Reputation: 7963
Quote:
Originally Posted by xmrkite View Post
Hello,

I have filenames like such: abc (e).doc
And I want to rename them to abc.doc

I have a directory full of files names like this. How can i do this using the sed command?

I have looked online for about 2-3 hours now and am frustrated that I can't find an answer.

Any help will be most appreciated
-Thanks
Three hours of searching should have turned up much on using sed to replace 'special' characters, especially the use of the backslash "\" to 'escape' them, and treat them as regular characters. Also the use of brackets for making a small regular-expression. The sed can be done as:
Code:
sed 's/[()]//g'
which will strip the () characters out of what you feed it. A small shell script can do it:
Code:
#!/bin/bash
for i in *.doc
do
        x=${i//[\(\)]/}
        echo "$i renames to: $x"
        mv $i $x
done
 
Old 06-08-2010, 06:31 PM   #3
xmrkite
Member
 
Registered: Oct 2006
Location: California, USA
Distribution: Mint 16, Lubuntu 14.04, Mythbuntu 14.04, Kubuntu 13.10, Xubuntu 10.04
Posts: 554

Original Poster
Rep: Reputation: 30
It's close. only issue is i need to have it remove those pesky e's between the ( and ).

Also, the space before the first ( needs to be removed.

Any ideas?

-Thanks
 
Old 06-08-2010, 06:49 PM   #4
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,617

Rep: Reputation: 7963Reputation: 7963Reputation: 7963Reputation: 7963Reputation: 7963Reputation: 7963Reputation: 7963Reputation: 7963Reputation: 7963Reputation: 7963Reputation: 7963
Quote:
Originally Posted by xmrkite View Post
It's close. only issue is i need to have it remove those pesky e's between the ( and ).

Also, the space before the first ( needs to be removed.

Any ideas?

-Thanks
Ideas? Yes, run the value of $x through another iteration, to remove whatever else you don't want, modifying the regex accordingly.
 
Old 06-08-2010, 06:57 PM   #5
xmrkite
Member
 
Registered: Oct 2006
Location: California, USA
Distribution: Mint 16, Lubuntu 14.04, Mythbuntu 14.04, Kubuntu 13.10, Xubuntu 10.04
Posts: 554

Original Poster
Rep: Reputation: 30
I understand where you're going here, but the issue is that it removes all of the letters I input in there. Some of the filenames have (e) and some have (L)...so when i put an e or an L in there, all e's and all L's from all the filenames are deleted.

I only want to remove the sequence of (e) and (L).

Not sure how to do that.
 
Old 06-08-2010, 08:47 PM   #6
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,617

Rep: Reputation: 7963Reputation: 7963Reputation: 7963Reputation: 7963Reputation: 7963Reputation: 7963Reputation: 7963Reputation: 7963Reputation: 7963Reputation: 7963Reputation: 7963
Quote:
Originally Posted by xmrkite View Post
I understand where you're going here, but the issue is that it removes all of the letters I input in there. Some of the filenames have (e) and some have (L)...so when i put an e or an L in there, all e's and all L's from all the filenames are deleted.

I only want to remove the sequence of (e) and (L).

Not sure how to do that.
And you won't if you don't try to learn how. Google for examples on Regular Expressions. I gave you the solution in my first post, and additional info in the second. Modify the x definition, and add another iteration if you need to. Think about what it's doing.
Code:
x=${i//[\(\)]/}
Since right now it's stripping out the () characters, you can obviously see them in the line above, right??? So modify it to handle a space and a letter.
Code:
x=${i//[ \(e\)]/}
x=${i//[ \(L\)]/}
There you go...
 
Old 06-09-2010, 12:52 AM   #7
rkski
Member
 
Registered: Jan 2009
Location: Canada
Distribution: CentOS 6.3, Fedora 17
Posts: 247

Rep: Reputation: 51
Code:
sed 's/\([[:alnum:]]*\)[[:space:]]*(.)\(\..*\)/\1\2/' file
Thats the sed part, you would have to feed in the files.
 
Old 06-09-2010, 09:38 AM   #8
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,617

Rep: Reputation: 7963Reputation: 7963Reputation: 7963Reputation: 7963Reputation: 7963Reputation: 7963Reputation: 7963Reputation: 7963Reputation: 7963Reputation: 7963Reputation: 7963
Quote:
Originally Posted by rkski View Post
Code:
sed 's/\([[:alnum:]]*\)[[:space:]]*(.)\(\..*\)/\1\2/' file
Thats the sed part, you would have to feed in the files.
Except the OP said he only wanted to strip out the (e) and (L) strings. If you get all the alpha-numeric, it'll grab those as well.
 
Old 06-09-2010, 11:12 AM   #9
xmrkite
Member
 
Registered: Oct 2006
Location: California, USA
Distribution: Mint 16, Lubuntu 14.04, Mythbuntu 14.04, Kubuntu 13.10, Xubuntu 10.04
Posts: 554

Original Poster
Rep: Reputation: 30
OK, I am trying, please don't think I'm just here looking for the easy answer. Sed is just so complex and because it can do so much, I'm having a hard time understanding why you put certain characters in certain places.

Anyhow, about your examples below:

x=${i//[ \(e\)]/}
x=${i//[ \(L\)]/}

They remove all the spaces in the filenames. While they do get rid of my (L) and (e) issue, now the files have no spaces, so that's not good. What am I doing wrong here?

-Thanks for the help
 
Old 06-09-2010, 02:54 PM   #10
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,617

Rep: Reputation: 7963Reputation: 7963Reputation: 7963Reputation: 7963Reputation: 7963Reputation: 7963Reputation: 7963Reputation: 7963Reputation: 7963Reputation: 7963Reputation: 7963
Quote:
Originally Posted by xmrkite View Post
OK, I am trying, please don't think I'm just here looking for the easy answer. Sed is just so complex and because it can do so much, I'm having a hard time understanding why you put certain characters in certain places.

Anyhow, about your examples below:

x=${i//[ \(e\)]/}
x=${i//[ \(L\)]/}

They remove all the spaces in the filenames. While they do get rid of my (L) and (e) issue, now the files have no spaces, so that's not good. What am I doing wrong here?

-Thanks for the help
In the examples, I'm not using SED at all. Those are regular expressions. And you posted earlier, about WANTING to get rid of the space. If you want to keep the space, remove it from the regex. Again, look at the example.
Code:
x=${i//[ \(e\)]/}
x=${i//[ \(L\)]/}
See the space?? Remove it:
Code:
x=${i//[\(e\)]/}
x=${i//[\(L\)]/}
There you go, now you'll keep the space. We can only base solutions here on the information you give us. So when you say you have files like "abc (e).doc", and you say you want to remove the parens and spaces, that's what we gave you. If you've got different requirements or different inputs, specify them, or experiment and learn how to do it yourself. Again, go to Google, and read up on regular expressions, and perhaps try to experiment. I know you say you're not looking for an easy answer, and I don't mean this to sound harsh, but you've been given all the tools and knowledge necessary, but don't seem to want to go forward with them, and learn how to apply them.

Also, what you're trying to do doesn't make much sense. If you have MANY files named "abc (e).doc", and you rename them to "abc.doc" (from your first post), you'll overwrite the first file with the second, and so on. Unless the file names are unique, you'll clobber each file with the next.
 
Old 06-09-2010, 03:14 PM   #11
xmrkite
Member
 
Registered: Oct 2006
Location: California, USA
Distribution: Mint 16, Lubuntu 14.04, Mythbuntu 14.04, Kubuntu 13.10, Xubuntu 10.04
Posts: 554

Original Poster
Rep: Reputation: 30
Hello and thanks.

I was trying to keep it simple. In my first post I asked how to change abc (e).doc to abc.doc.

While you did tell me a way to do it, it did not work for all my files because some have spaces that I do want to keep. I guess I should have stated that i wanted to have file abc def (e.)doc become abc def.doc

I just figured it out (greatly with your help). I took out the space from your regex expression and then used the rename command to change " .doc" to just ".doc"


Thanks again for the help everyone. I will be more careful in the future when asking questions.
 
Old 06-09-2010, 03:30 PM   #12
rkski
Member
 
Registered: Jan 2009
Location: Canada
Distribution: CentOS 6.3, Fedora 17
Posts: 247

Rep: Reputation: 51
Quote:
Code:

sed 's/\([[:alnum:]]*\)[[:space:]]*(.)\(\..*\)/\1\2/' file

Thats the sed part, you would have to feed in the files.

The above does the job even if the input is "abc def (e).doc"
It gives abc def.doc
 
  


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
Get a list of delimited filenames from a text file (sed?) Ksearch Linux - Newbie 14 06-30-2009 04:51 PM
How to search multiple files w/ SED then echo back the filenames and results??? hgate73 Programming 7 04-06-2009 03:11 PM
bash script with grep and sed: sed getting filenames from grep odysseus.lost Programming 1 07-17-2006 11:36 AM
please help me remove the spaces in filenames in a directory! asilentmurmur Linux - Newbie 3 07-14-2006 07:17 PM
need help with script to remove all metachars from filenames BrianK Programming 5 08-20-2005 11:10 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 02:35 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
Open Source Consulting | Domain Registration