The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


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

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
insert text into the middle of a original file mopimp Shell Programming and Scripting 1 03-26-2006 07:11 AM
ftp - get file and keep original timestamp? frustrated1 Shell Programming and Scripting 2 03-15-2006 09:31 AM
accessing o an exact line in a file walnut Shell Programming and Scripting 2 02-23-2006 12:31 PM
how to find the exact pattern from a file? surjyap Shell Programming and Scripting 7 12-05-2005 05:00 AM
Help with multiple file rename - change case of part of file name steve7 UNIX for Dummies Questions & Answers 7 06-30-2005 10:41 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-03-2006
Registered User
 

Join Date: Mar 2006
Posts: 4
Stumble this Post!
Rename a file if I don't know its exact original name?

Hi everyone,

I will be downloading a file every day with the name in this format

SCA20060303_17514_IN.TXT

And I need to rename it to

SCA20060303_IN.TXT

Where "20060303" is the current date, and the "_17514" part will always be a 5-digit number that I will NOT know beforehand. I just need to take it out.

I've always used the mv filename1 filename2 command to rename files but how can I do this if I don't know the exact original name of the file?

Thanks,

Ryan
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 03-03-2006
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 3,002
Stumble this Post!
Code:
echo 'SCA20060303_17514_IN.TXT' | sed 's#^\([^_][^_]*\)_[0-9][0-9]*\(.*\)#\1\2#'
Reply With Quote
  #3 (permalink)  
Old 03-03-2006
Registered User
 

Join Date: Mar 2006
Posts: 4
Stumble this Post!
Thank you, but I won't know the file name beforehand. To get the file, I do

mget SCA*.TXT

How can I do this without knowing the original name?
Reply With Quote
  #4 (permalink)  
Old 03-03-2006
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 3,002
Stumble this Post!
Quote:
Originally Posted by propel
Thank you, but I won't know the file name beforehand. To get the file, I do

mget SCA*.TXT

How can I do this without knowing the original name?
well.. 'mget' the files AND rename them all afterwards.

OR

look into ftp's [if that's the client you're using] "nmap" command if one exists for your ftp client:
Code:
ftp host
    nmap $1_$2_$3 $1_$3
    mget SCA*.TXT

Last edited by vgersh99; 03-03-2006 at 11:56 AM.
Reply With Quote
  #5 (permalink)  
Old 03-03-2006
Registered User
 

Join Date: Feb 2006
Location: Southern England
Posts: 102
Stumble this Post!
Code:
for f in SCA????????_?????_IN.TXT;do
      echo mv $f ${f%%_*}_IN.TXT
done
ksh
obviously remove the echo
Reply With Quote
  #6 (permalink)  
Old 03-03-2006
Registered User
 

Join Date: Mar 2006
Posts: 4
Stumble this Post!
Thank you all for your help. vgersh99, that nmap command is crazy!
Reply With Quote
  #7 (permalink)  
Old 03-03-2006
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 3,002
Stumble this Post!
Quote:
Originally Posted by propel
Thank you all for your help. vgersh99, that nmap command is crazy!
everything is relative in this world!
do you understand what 'nmap' does?
did you consider the pros and cons of either of the methods?
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 11:43 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0