Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
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.
Hey, I am decent with Linux but I have hit a speed bump. I am trying to get a bash, korn, shell, script or any other type of solution that replaces ( ) parenthesis characters with blank spaces, underscores, or just removes them completely. I have a large list of files in a directory and some of them contain parenthesis and I need them gone or changed. Can anybody please help! I have tried to write scripts and looked online to modify other scripts that remove spaces and other characters but have came up short. ANY help would be greatly appreciated.
Here is a couple of my attempts.
#!/bin/sh
REPLACE_CHAR=":"
for FILE in `find \temp23\ -type f`
do
echo $FILE | grep $REPLACE_CHAR > /dev/null
if [ "$(" == "0" ]
then
mv $FILE `echo $FILE | sed s/:/-/g`
fi
done
----------------------------------------------------------------------
#!/bin/bash
# badname.sh
# Delete filenames in current directory containing bad characters.
for filename in J:\Project Analysis Record\Cole\ADNI\temp23
do
badname=`echo "$filename" | sed -n /[\+\{\;\"\\\=\?~\(\)\<\>\&\*\|\$]/p`
# badname=`echo "$filename" | sed -n '/[+{;"\=?~()<>&*|$]/p'` also works.
# Deletes files containing these nasties: + { ; " \ = ? ~ ( ) < > & * | $
#
rm $badname 2>/dev/null
# ^^^^^^^^^^^ Error messages deep-sixed.
done
# Now, take care of files containing all manner of whitespace.
find . -name "* *" -exec rm -f {} \;
# The path name of the file that _find_ finds replaces the "{}".
# The '\' ensures that the ';' is interpreted literally, as end of command.
You don't need a complicated bash loop, rename does what you want easily :
rename -vn 's@stringToChange@resultingString@' *
To replace left parenthesis by an underscore, by example : rename -vn 's@\(@_@' *
You can also remove them : rename -vn 's@\(@@' *
and idem for the right ones : rename -vn 's@\)@_@' *
NB : The -n option is for tests purposes as it won't change your file, but you can see what should have changed (thanks to the -v option)
So when you have found what you like the most remove the -n (and -v if you want)
==> rename 's@ \(@\(>°°\)> @' *
rename 's@\)@ \<(°°<\) @' *
Just an additional note (pardon my bad english ); if you are trying to change a character inside the file itself, and not in the name of the file, you could use the 'replace' script that comes with MySQL (if you have it)
e.g.
replace "a" "[A]" -- my_file.txt
that would make the word 'calendar' become 'c[A]lend[A]r' inside the file.
^ ou sans mySQL, utiliser la commande sed avec la même syntaxe que rename :
edit : "or without mySQL, you can use the sed command which has the same syntax than rename :"
Hey, I tried those suggestions. I didnt quite understand them. However, I just need to replace the parenthesis ( ) in a directory. The filenames themselves have ( ) in them that cannot be there when I run a script. I need them changed or removed. Thanks for the suggestions.
are you in the good folder when you run these ???
the "*" at the end of the lines specify on what file it will apply, so here it is on every file of the current directory...
Other than this I don't see where could the problem be
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.