LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   change a specific characters to capital in a specific text (https://www.linuxquestions.org/questions/linux-newbie-8/change-a-specific-characters-to-capital-in-a-specific-text-848079/)

ramzaher 12-02-2010 11:58 PM

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

grail 12-03-2010 12:55 AM

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??

ramzaher 12-03-2010 01:14 AM

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 ).

grail 12-03-2010 01:50 AM

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.

ramzaher 12-03-2010 02:16 AM

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 )

grail 12-03-2010 03:07 AM

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?

ramzaher 12-03-2010 03:15 AM

ok give me another one but nt simple way to do it

grail 12-03-2010 03:23 AM

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??

ramzaher 12-03-2010 03:30 AM

aha mm i can do it in a loop using the script but i want something smarter !

ramzaher 12-03-2010 03:32 AM

mm sorry i meant to say give another one but IN simple way

grail 12-03-2010 04:11 AM

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.

ramzaher 12-03-2010 04:40 AM

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

grail 12-03-2010 04:50 AM

Nice ... I had not used the suffix option previously ... nice.


All times are GMT -5. The time now is 05:19 PM.