LinuxQuestions.org
Review your favorite Linux distribution.
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 12-02-2010, 11:58 PM   #1
ramzaher
LQ Newbie
 
Registered: Nov 2010
Posts: 25

Rep: Reputation: 0
change a specific characters to capital in a specific text


for example

else {

for fileDOC in $location/*.doc
do
echo $fileDOC | tr [a-z] [A-Z] > $fileDOC
cp $fileDOC ./DOC

done
}

fi

the fileDOC return e.g
testDir/test1Dir/test2Dir//doc1.doc
testDir/test1Dir/test2Dir//doc2.doc
testDir/test1Dir/test2Dir//doc3.doc
testDir/test1Dir/test2Dir//doc5.doc

what i want is to change doc1 and doc2 and doc3 to capital (the name of the file) and return it to the variable fileDOC
 
Old 12-03-2010, 12:55 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
SO I have a few issues:

1. Please use [code][/code] tags around your code
Quote:
echo $fileDOC | tr [a-z] [A-Z] > $fileDOC
2. a. What were you expecting here?
b. This will now clobber anything previously inside the file. Is this what you wanted?
Quote:
what i want is to change doc1 and doc2 and doc3 to capital (the name of the file) and return it to the variable fileDOC
3. You might need to explain this one further as the fileDOC variable is not having anything returned to it??
 
Old 12-03-2010, 01:14 AM   #3
ramzaher
LQ Newbie
 
Registered: Nov 2010
Posts: 25

Original Poster
Rep: Reputation: 0
the program is to get files (in any directory specified by the path) that end with extension .doc and change the name of these files to capital (just the name ).
 
Old 12-03-2010, 01:50 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
So as you are on Ubuntu you could just use:
Code:
rename -n 's/\([[:alpha:]]+\)\(\..*\)$/\U\1\2/' $location/*.doc
Untested but something like that. When you are happy just remove '-n' and the change will take affect.
 
Old 12-03-2010, 02:16 AM   #5
ramzaher
LQ Newbie
 
Registered: Nov 2010
Posts: 25

Original Poster
Rep: Reputation: 0
mm ok just help with this if u can

Code:
 capital=$( echo $fileDOC | tr '[a-z].doc' '[A-Z].doc' )
so if i have doc1.doc
i want to change it to DOC1.doc
how i can do it using the code above (using tr )
 
Old 12-03-2010, 03:07 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
My understanding of tr is that it is a character by character translation and has no smarts about regex, hence your example will never work.

Is there a reason we cannot use another tool?
 
Old 12-03-2010, 03:15 AM   #7
ramzaher
LQ Newbie
 
Registered: Nov 2010
Posts: 25

Original Poster
Rep: Reputation: 0
ok give me another one but nt simple way to do it
 
Old 12-03-2010, 03:23 AM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
Quote:
ok give me another one but nt simple way to do it
I am sorry ... you want a more complicated way to do it? Isn't that defeating the idea of a script which is to simplify things??
 
Old 12-03-2010, 03:30 AM   #9
ramzaher
LQ Newbie
 
Registered: Nov 2010
Posts: 25

Original Poster
Rep: Reputation: 0
aha mm i can do it in a loop using the script but i want something smarter !
 
Old 12-03-2010, 03:32 AM   #10
ramzaher
LQ Newbie
 
Registered: Nov 2010
Posts: 25

Original Poster
Rep: Reputation: 0
mm sorry i meant to say give another one but IN simple way
 
Old 12-03-2010, 04:11 AM   #11
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
Well I would have to say the rename option is probably the most simple way. Other than that I think you would have to use a loop.
Something like:
Code:
for FILE in $location/*
do
    TMP_FILE=${FILE##*/}
    TMP_EXT=${TMP_FILE##*.}
    TMP_FILE=${TMP_FILE%%.*}

    mv ${FILE} $location/${TMP_FILE^^}.$TMP_EXT
done
If there are spaces in the names you may want to put quotes around the variables.
 
Old 12-03-2010, 04:40 AM   #12
ramzaher
LQ Newbie
 
Registered: Nov 2010
Posts: 25

Original Poster
Rep: Reputation: 0
thanks grail for the solution ...i found another solution using basename command

e.g: filename.doc

[cod] basename filename.doc .doc | tr [a-z] [A-Z] [\code]
will return FILENAME then i can combine it to .doc to get FILENAME.doc

ofcourse i may use a loop to get all the file in a directory that end with .doc and change it
 
Old 12-03-2010, 04:50 AM   #13
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
Nice ... I had not used the suffix option previously ... nice.
 
  


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
Way to make autofs mount to specific USB flash drive to specific folder? utahnix Linux - Software 2 11-24-2010 05:27 PM
[SOLVED] Change to capital first letter of every word over specific column cgcamal Programming 10 05-02-2010 07:22 AM
SED - display text on specific line of text file 3saul Linux - Software 3 12-29-2005 04:32 PM
How to find and change a specific text in a text file by using shell script Bassam Programming 1 07-18-2005 07:15 PM
Non ASCII (language specific) characters in filenames? milicic.marko Linux - General 2 03-19-2005 05:15 PM

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

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