How do you remove Parenthesis from filenames using sed
Linux - SoftwareThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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).
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.
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?
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.