The UNIX and Linux Forums

The UNIX and Linux Forums (http://www.unix.com/index.php)
-   Shell Programming and Scripting (http://www.unix.com/shell-programming-and-scripting/)
-   -   Truncate multiple file extensions (http://www.unix.com/shell-programming-and-scripting/42190-truncate-multiple-file-extensions.html)

prvnrk 08-24-2007 12:13 AM

Truncate multiple file extensions
 
Hi,

I have files with names like file1.txt.txt.txt.txt and file2.txt.txt.txt.txt.txt............ (random infinite number of .txt exist).

how to truncate (mv) their names to ones with single .txt extension like file1.txt and file1.txt ? In other words, how to extract the filename upto first extension part?

Txs
Prvn

namishtiwari 08-24-2007 01:03 AM

Do something like this---

mv file1.txt.txt* file1.txt
mv file2.txt.txt* file2.txt

or

sed s/file1.txt.txt*/file1.txt

Thanks
Namish

summer_cherry 08-24-2007 01:06 AM

!!!!!!!!!!!!
 
Hi try this one:

Code:

echo "number"
read num
who | nawk -v n=$num 'BEGIN{
str="file"
for (i=1;i<=n;i++)
str=sprintf("%s%s",str,".txt")
}
END{
print str
printf("%stxt\n",substr(str,1,index(str,".")))
}'


prvnrk 08-24-2007 01:13 AM

Txs namishtiwari,

But i have to do this for thousands of files in a directory so i cant use hardcoded filename. i will be using for loop for reading each file name. So, here how could i extract the "filename with first extension" out of "filename with infinite extensions"?

txs summer_cherry, here i dont know what is "number"..

namishtiwari 08-24-2007 01:28 AM

Quote:

Originally Posted by prvnrk (Post 302133546)
Txs namishtiwari,

But i have to do this for thousands of files in a directory so i cant use hardcoded filename. i will be using for loop for reading each file name. So, here how could i extract the "filename with first extension" out of "filename with infinite extensions"?

txs summer_cherry, here i dont know what is "number"..

Summer_cherry is just doing a echo of number,nothing to do with number that is not a variable.Whatever you do with echo that is excatly printed again.If you do not understand the code let me know i will tell you.

Thanks
Namish

prvnrk 08-24-2007 01:44 AM

Namish,

I know what " echo "number" " does and there's nothing hard to understand there. But his code expects again a "num".

I'm not sure if you read my post fully and again i repeat "how could i extract the "filename with first extension" out of "filename with infinite extensions"?" e.g., how to extract "file1.txt" from file1.txt.txt.txt..........? (no hardcoding of filenames with wild char pls..)


Regards,

Prvn

namishtiwari 08-24-2007 01:48 AM

suppose you enterd num as 5,then it will print a file that is having a extension of
file1.txt.txt.txt.txt.txt
and reduce it to
file1.txt


All times are GMT -4. The time now is 07:04 PM.

Linux and Unix Supported by: vBulletin
Search Engine Optimisation provided by DragonByte SEO v1.1.4 (Pro) - vBulletin Mods & Addons Copyright © 2014 DragonByte Technologies Ltd.
The UNIX and Linux Forums Content Copyright ©1993-2013. All Rights Reserved.
Forum Operations by The UNIX and Linux Forums