Rename a file sequencially in UNIX


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Rename a file sequencially in UNIX
# 1  
Old 03-12-2010
Rename a file sequencially in UNIX

I need to be able to look for the last file in a dirctory in UNIX,
the file satrt with EFT
so this work
Code:
file=$(ls -tr $EFT*.dat | tail -1) # Select the latest file

my last file is EFT1234.dat
then I need to be able to get a file that I just ftp and rename with this
name EFT1234.dat but plus 1
Code:
file=$(ls -tr $EFT*.dat | tail -1) # Select the latest file
filesuffix=${file%.dat} # Remove the extension
let numlength=${#filesuffix}-${#EFT} # Detemine the length of the numeric
typeset -R$numlength filenum=$filesuffix # Remove the number from the right
get "$file $numlength $filenum"

I also try
Code:
filesuffix=${EFT%.dat} # Remove the extension

I am getting tjhe following:
The 03121000.DSB is the file that I just FTP and I need to rename EFT1235.dat
Code:
+ 0<<
quote user banftp_finaid
quote pass gvf1!3Mk
cd orgs
cd "Financial Aid"
cd MIIS
cd 0910
cd FTP
get 03121000.DSB
# The start of a filename
file=+ tail -1
+ tail -1
+ ls -tr 3526_337092.dat EFT1234.dat HISTOGRAM200910.dat Histogram_200910.dat eluppdtop.dat idoc.dat lockbox_payments.dat
+ ls -tr 3526_337092.dat EFT1234.dat HISTOGRAM200910.dat Histogram_200910.dat eluppdtop.dat idoc.dat lockbox_payments.dat
EFT1234.dat # Select the latest file
filesuffix= # Remove the extension
let numlength=0-0 # Detemine the length of the numeric
typeset -R filenum= # Remove the number from the right
get " "


Last edited by rechever; 03-12-2010 at 02:42 PM.. Reason: code tags, please...
# 2  
Old 03-12-2010
If only numbers are the sequential number

Perhaps a simpler way of getting to the number
Code:
echo EFT1235.dat | tr -cd '[:digit:]'
1235

the tr command with the -d deletes characters that match
but the -c says to use complement of what I provide
the [:digit:] is a special class for numbers; there are many others tooSmilie
# 3  
Old 03-12-2010
Not sure your problem, but if you need rename a file, you can try this code:

Code:
$ echo "EFT1234.dat" |awk '{a=substr($0,4,4)+1; print "EFT" a ".dat"}'
EFT1235.dat

# 4  
Old 03-12-2010
Code:
echo "EFT1234.dat" |perl -lne '/([A-Z]+)([0-9]+).*/; print $1.($2+1).".dat";'

# 5  
Old 03-14-2010
or more shorter:-

Code:
echo "EFT1234.dat" | perl -lpe 's/\d+/$& + 1/e;'

BR
# 6  
Old 03-15-2010
Quote:
Originally Posted by dennis.jacob
[CODE]

Code:
echo "EFT1234.dat" |perl -lne '/([A-Z]+)([0-9]+).*/; print $1.($2+1).".dat";'

[code]
Excuse my ignorance, but If I execute this command in the command line in UNIX works, but how I put this in a shl script?

I have the following
[code]
file=$(ls -tr $EFT*.dat | tail -1) # Select the latest fil e
file=$(ls -tr $EFT*.dat | tail -1) # Select the latest fil e
file |perl -lne '/([A-Z]+)([0-9]+)
let filenum= print $1.($2+1).".dat"
[code]
EFT1234.dat # Select the latest fil e (this is correct)
but this is not working
file |perl -lne '/([A-Z]+)([0-9]+)
let filenum= print .(+1).".dat"
I want to be able to store EFT1234.dat in a variable (file)
then
[code]
file |perl -lne '/([A-Z]+)([0-9]+)
let filenum= print .(+1).".dat"
and ended with EFT1235.dat
[code]
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Help with Unix file rename

Hi All, I have a unix file which is ceated with variable name, for example: FIRST_FILE_3456 and next time it will be FIRST_FILE_3457 and so on. I need to rename this file like PREV_FIRST_FILE_XXXX where XXXX is the variable part of the file FOR Example 3456 or 3457, etc. Please help to... (4 Replies)
Discussion started by: Mohammad T Khan
4 Replies

2. Shell Programming and Scripting

How to rename a file even when it shows permission Denied in Unix

While executing a script, I am not being able to able to create a file as the file with the same name already exists. That existing file is not getting overwritten as I am not the owner of the file. So, Neither am I able to rename the file nor delete the existing file, so as to get my file created.... (2 Replies)
Discussion started by: Haimanti
2 Replies

3. Shell Programming and Scripting

Multiple file rename (change in filename in unix with single command

Dear All, Please help ! i ham having 300 file with E.G. PMC1_4567.arc in seq. like PMC1_4568.arc,PMC1_4569.arc ...n and so on.. i want all those file to be rename like PMC_4567.arc ,PMC_4568.arc .. mean i want to remove 1 from first file name .. pls help.. (6 Replies)
Discussion started by: moon_22
6 Replies

4. UNIX for Advanced & Expert Users

Unix Command to rename a file in a zipped folder

Hi, I am into automation work. Is there any UNIX command to rename a file in a zipped folder, without unzipping it..??? Thanks in advance.. (1 Reply)
Discussion started by: Victoria.Sam
1 Replies

5. Shell Programming and Scripting

How to look for the last file and rename it (sequencially, increment by one)

I need to Ftp a file to a UNIX directory from a windows directory, the file needs to be put sequencially in the UNIX directory , for example: I get the file from the windows directory with the following name. EFT###.DAT, the script needs to look in the UNIX directory and if there are files, for... (5 Replies)
Discussion started by: rechever
5 Replies

6. UNIX for Advanced & Expert Users

How UNIX/AIX handles a file deep down, say it's being read while another one tries to rename it?

Hi Thinkers, On AIX 5.3, we have a monitor program that reads the log file and searching for a certain string pattern that we define(say "transactionException"), if it sees it then it will raise an alert by sending an email. Because the log file XXX.log is rolling into XXX.log.0, XXX.log.1,... (2 Replies)
Discussion started by: TheGunMan
2 Replies

7. Shell Programming and Scripting

copy/rename file as date() unix/shell

File.jpg I want to copy and rename this as 2008-12-02.jpg I tried this copy File.jpg date '%y-%m-%d-%H:%M:%S'.jpg This doesnt work.... what do i do? (1 Reply)
Discussion started by: hdogg
1 Replies

8. Shell Programming and Scripting

[Unix] a dos style rename wont work

Hey guys i'm creating a dos style rename script, so if a user types say q14.* as the 1st param and b14.* as the 2nd and will rename all q14 files to b14 but keep the extensions, so i've developed nearly the full script "i think", if i use echo(echo "if $1 had been renamed it would now be... (3 Replies)
Discussion started by: fblade1987
3 Replies

9. UNIX for Dummies Questions & Answers

unix rename

Whats the command in unix to do this ; rename a bunch (3000) of files called ; 0001.jpg 0002.jpg 0003.jpg 0004.jpg to 0001_LRG.jpg 0002_LRG.jpg 0003_LRG.jpg etc. i.e. adding the extension _LRG to the end of the filename ? (3 Replies)
Discussion started by: signprint
3 Replies

10. UNIX for Advanced & Expert Users

How Do I rename a unix file contianing spacing?

How can I rename a unix file that has a spacing in in the charactyers? For example, I want to rename or move a file called "My OldFile.txt" to "MyNewFile.txt" or "My_New_file.txt" Please help (7 Replies)
Discussion started by: frankyranky
7 Replies
Login or Register to Ask a Question