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 and shell scripting languages 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 11:11 AM
ftp - get file and keep original timestamp? frustrated1 Shell Programming and Scripting 2 03-15-2006 01:31 PM
accessing o an exact line in a file walnut Shell Programming and Scripting 2 02-23-2006 04:31 PM
how to find the exact pattern from a file? surjyap Shell Programming and Scripting 7 12-05-2005 09:00 AM
Help with multiple file rename - change case of part of file name steve7 UNIX for Dummies Questions & Answers 7 06-30-2005 02:41 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 03-03-2006
propel propel is offline
Registered User
  
 

Join Date: Mar 2006
Posts: 4
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
  #2 (permalink)  
Old 03-03-2006
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,131

Code:
echo 'SCA20060303_17514_IN.TXT' | sed 's#^\([^_][^_]*\)_[0-9][0-9]*\(.*\)#\1\2#'

  #3 (permalink)  
Old 03-03-2006
propel propel is offline
Registered User
  
 

Join Date: Mar 2006
Posts: 4
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?
  #4 (permalink)  
Old 03-03-2006
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,131
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 03:56 PM..
  #5 (permalink)  
Old 03-03-2006
bigearsbilly bigearsbilly is offline
Registered User
  
 

Join Date: Feb 2006
Location: Southern England
Posts: 104

Code:
for f in SCA????????_?????_IN.TXT;do
      echo mv $f ${f%%_*}_IN.TXT
done

ksh
obviously remove the echo
  #6 (permalink)  
Old 03-03-2006
propel propel is offline
Registered User
  
 

Join Date: Mar 2006
Posts: 4
Thank you all for your help. vgersh99, that nmap command is crazy!
  #7 (permalink)  
Old 03-03-2006
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,131
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?
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 08:37 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0