Truncate multiple file extensions | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Truncate multiple file extensions

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 08-24-2007
prvnrk prvnrk is offline
Registered User
 
Join Date: Jul 2007
Last Activity: 21 September 2014, 12:49 PM EDT
Posts: 213
Thanks: 10
Thanked 5 Times in 5 Posts
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
Sponsored Links
    #2  
Old 08-24-2007
namishtiwari namishtiwari is offline Forum Advisor  
Registered User
 
Join Date: Aug 2007
Last Activity: 2 October 2012, 1:31 PM EDT
Location: Bangalore
Posts: 390
Thanks: 0
Thanked 0 Times in 0 Posts
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
Sponsored Links
    #3  
Old 08-24-2007
summer_cherry summer_cherry is offline Forum Advisor  
Registered User
 
Join Date: Jun 2007
Last Activity: 23 July 2014, 3:17 AM EDT
Location: Beijing China
Posts: 1,294
Thanks: 0
Thanked 24 Times in 24 Posts
!!!!!!!!!!!!

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,".")))
}'

    #4  
Old 08-24-2007
prvnrk prvnrk is offline
Registered User
 
Join Date: Jul 2007
Last Activity: 21 September 2014, 12:49 PM EDT
Posts: 213
Thanks: 10
Thanked 5 Times in 5 Posts
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"..
Sponsored Links
    #5  
Old 08-24-2007
namishtiwari namishtiwari is offline Forum Advisor  
Registered User
 
Join Date: Aug 2007
Last Activity: 2 October 2012, 1:31 PM EDT
Location: Bangalore
Posts: 390
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by prvnrk View Post
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
Sponsored Links
    #6  
Old 08-24-2007
prvnrk prvnrk is offline
Registered User
 
Join Date: Jul 2007
Last Activity: 21 September 2014, 12:49 PM EDT
Posts: 213
Thanks: 10
Thanked 5 Times in 5 Posts
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
Sponsored Links
    #7  
Old 08-24-2007
namishtiwari namishtiwari is offline Forum Advisor  
Registered User
 
Join Date: Aug 2007
Last Activity: 2 October 2012, 1:31 PM EDT
Location: Bangalore
Posts: 390
Thanks: 0
Thanked 0 Times in 0 Posts
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
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
List directory name (only once) after multiple file extensions found mmab Shell Programming and Scripting 8 11-11-2011 07:39 AM
Replacing multiple extensions wangkc Shell Programming and Scripting 1 06-13-2011 07:57 AM
changing extensions for multiple files at once Straitsfan UNIX for Dummies Questions & Answers 1 04-22-2009 03:39 PM
Creating multiple extensions chssastry Shell Programming and Scripting 2 06-24-2008 12:52 AM
Stripping out extensions when file has multiple dots in name Nemelis Shell Programming and Scripting 8 05-14-2008 08:12 AM



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