LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 01-24-2008, 02:12 PM   #1
xberetta21
LQ Newbie
 
Registered: Jan 2008
Posts: 9

Rep: Reputation: 0
Angry Script/Solution to problem?!?! PLEASE HELP


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.

exit 0
 
Old 01-24-2008, 03:28 PM   #2
Shautieh
Member
 
Registered: Sep 2006
Distribution: Ubuntu
Posts: 64

Rep: Reputation: 15
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@\)@ \<(°°<\) @' *
 
Old 01-24-2008, 03:55 PM   #3
xberetta21
LQ Newbie
 
Registered: Jan 2008
Posts: 9

Original Poster
Rep: Reputation: 0
Wink Thank YOu!

Thanks Ill give it a try first thing tomarrow mouring. I am on my way out. I figured it was a smaller script that I was making. Thanks again.
 
Old 01-24-2008, 04:10 PM   #4
jespinal
LQ Newbie
 
Registered: Jan 2008
Location: Dominican Republic
Distribution: Slackware 13.1
Posts: 3
Blog Entries: 2

Rep: Reputation: 0
Smile

Hi there

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.
 
Old 01-25-2008, 09:47 AM   #5
Shautieh
Member
 
Registered: Sep 2006
Distribution: Ubuntu
Posts: 64

Rep: Reputation: 15
^ 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 :"

sed -i 's@a@\[A\]@' file.txt


Last edited by Shautieh; 01-25-2008 at 11:46 AM.
 
Old 01-25-2008, 10:19 AM   #6
xberetta21
LQ Newbie
 
Registered: Jan 2008
Posts: 9

Original Poster
Rep: Reputation: 0
Help

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.
 
Old 01-25-2008, 11:48 AM   #7
Shautieh
Member
 
Registered: Sep 2006
Distribution: Ubuntu
Posts: 64

Rep: Reputation: 15
^ the example I gave before do what you want... Didn't you get anything of what I said ?

I also gave examples you could use almost word for word.. =>
rename 's@\(@_@' *
rename 's@\)@_@' *
 
Old 01-25-2008, 12:18 PM   #8
xberetta21
LQ Newbie
 
Registered: Jan 2008
Posts: 9

Original Poster
Rep: Reputation: 0
Thanks.

Ok I ran those commands. But nothign happens. I appreciate your input, do you know any of possible solutions?
 
Old 01-25-2008, 03:40 PM   #9
Shautieh
Member
 
Registered: Sep 2006
Distribution: Ubuntu
Posts: 64

Rep: Reputation: 15
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
 
Old 01-28-2008, 09:56 AM   #10
xberetta21
LQ Newbie
 
Registered: Jan 2008
Posts: 9

Original Poster
Rep: Reputation: 0
Wink AHhhhhh

It keeps telling me " Badly Placed ( )" Thats it..... Very frusterating. Thanks for the input so far. Any other solutions you can think of?
 
  


Reply



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
LXer: Sometimes a CGI script is the most elegant solution LXer Syndicated Linux News 0 03-02-2007 12:31 PM
Need a Document.all solution which transform any ma js script. nadavvin Linux - Software 1 11-10-2006 11:55 AM
Script as a backup Solution shawnbishop Linux - Software 3 04-14-2006 08:39 PM
LXer: The New Shop-Script PRO, Open Source PHP Ecommerce Solution ... LXer Syndicated Linux News 0 02-07-2006 01:46 PM
Build Passwd for personal access script, I want to solution. somphob Linux - General 2 08-01-2004 04:25 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01: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