separate numbers in a file

I have this command in a shl (UNIX) to find the lates file that start with EMT in a directory

file=$(ls -tr $EMT*.dat | tail -1) # Select the latest file

It finds:
EMT345.dat
then I have to be able to separate EMT AND the numbers 345 and stored 345 in a variable and incremented, so my new file that I just FTP will be name EMT346.DAT

Try with the following ,

file="EMT345.dat"
new=`sed -r "s/^[^0-9]+([0-9]+).+$/\1/" <<<$file`
echo $new
let new=$new+1;
echo $new
>"EMT$new.dat"
         Here the file have the value "EMT345.dat".Using sed I have store the number in a variable and incremented it by one then created a new file using the new number .
newfilename="EMT$(( $(echo $file | sed 's/EMT\([0-9]*\)\.dat/\1/g') + 1)).dat"

see the following PERL code:

my $file= `ls -tr |  tail -1` ;# Select the latest file
print $file;

my $val = $file;
$val =~ s/EMT([0-9]+)[.][a-z]*/\1/ ;


$val=$val+1;
$file = "EMT".$val.".pl";

print $file;

Output:

EMT345.pl
EMT346.pl

Using the above code you can achieve you requirement....

I try this code
This is in the SHL script (it is actually EFT)
is the prl code, can I put perl code in a shl script?
Anyway it is not working..

# The start of a filename
file=$(ls -tr $EFT*.dat | tail -1) # Select the latest file
new=sed -r "s/^[^0-9]+([0-9]+).+$/\1/" <<<$file
echo $new
let new=$new+1;
echo $new
>"EFT$new.dat"
!EOF
# End of FTP Process
RESULTS 

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
new=sed -r "s/^[^0-9]+([0-9]+).+$/\1/" <<<
echo 
let new=+1;
echo 
>"EFT.dat"

Try with the following ,

file="EMT345.dat"
new=`sed -r "s/^[^0-9]+([0-9]+).+$/\1/" <<<$file`
echo $new
let new=$new+1;
echo $new
>"EMT$new.dat"

Here the file have the value "EMT345.dat".Using sed I have store the number in a variable and incremented it by one then created a new file using the new number .
[/quote]

newfilename=EMT$(($(echo $file | tr -cd [0-9])+1)).dat

On an unrelated issue, I'm a bit puzzled over the use of

ls -tr | tail -1

instead of the shorter, clearer and more intuitive

ls -t | head -1

I know it's not a big deal; I'm just picking a nit. :slight_smile:

Cheers,
Alister

This topic has been discussed with your another post.

Is this BASH? If so, there's no need for anything other than putting a little builtin parameter expansion on the variable.

${VAR:3:3}

This extracts a substring from the variable, in this case the text starting after the first three characters and ending after three more characters.

# VAR1=EMT346.DAT
# echo ${VAR1:3:3}
346
# VAR2=$((${VAR1:3:3}+1))
# echo $VAR2
347
#

or...

# VAR2=EMT$((${VAR1:3:3}+1)).DAT
# echo $VAR2
EMT347.DAT