LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   How do you remove Parenthesis from filenames using sed (https://www.linuxquestions.org/questions/linux-software-2/how-do-you-remove-parenthesis-from-filenames-using-sed-812975/)

xmrkite 06-08-2010 05:57 PM

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

TB0ne 06-08-2010 06:18 PM

Quote:

Originally Posted by xmrkite (Post 3997131)
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


xmrkite 06-08-2010 06:31 PM

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

TB0ne 06-08-2010 06:49 PM

Quote:

Originally Posted by xmrkite (Post 3997157)
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.

xmrkite 06-08-2010 06:57 PM

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.

TB0ne 06-08-2010 08:47 PM

Quote:

Originally Posted by xmrkite (Post 3997179)
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...

rkski 06-09-2010 12:52 AM

Code:

sed 's/\([[:alnum:]]*\)[[:space:]]*(.)\(\..*\)/\1\2/' file
Thats the sed part, you would have to feed in the files.

TB0ne 06-09-2010 09:38 AM

Quote:

Originally Posted by rkski (Post 3997415)
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.

xmrkite 06-09-2010 11:12 AM

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

TB0ne 06-09-2010 02:54 PM

Quote:

Originally Posted by xmrkite (Post 3997983)
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.

xmrkite 06-09-2010 03:14 PM

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.

rkski 06-09-2010 03:30 PM

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


All times are GMT -5. The time now is 08:10 AM.