separate numbers in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting separate numbers in a file
# 1  
Old 03-16-2010
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
Code:
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

Last edited by pludi; 03-16-2010 at 09:59 AM..
# 2  
Old 03-16-2010
Try with the following ,

Code:
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 .
# 3  
Old 03-16-2010
Code:
newfilename="EMT$(( $(echo $file | sed 's/EMT\([0-9]*\)\.dat/\1/g') + 1)).dat"

# 4  
Old 03-16-2010
MySQL

see the following PERL code:

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....
# 5  
Old 03-16-2010
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..

Code:
# 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 ,

Code:
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]

Last edited by Scott; 03-16-2010 at 08:27 PM.. Reason: Fixed code tags
# 6  
Old 03-16-2010
Code:
newfilename=EMT$(($(echo $file | tr -cd [0-9])+1)).dat

On an unrelated issue, I'm a bit puzzled over the use of
Code:
ls -tr | tail -1

instead of the shorter, clearer and more intuitive
Code:
ls -t | head -1

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

Cheers,
Alister
# 7  
Old 03-16-2010
This topic has been discussed with your another post.

https://www.unix.com/shell-programmin...ally-unix.html
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Adding (as in arithmetic) to numbers in columns in file, and writing new file with new numbers

Hi again. Sorry for all the questions — I've tried to do all this myself but I'm just not good enough yet, and the help I've received so far from bartus11 has been absolutely invaluable. Hopefully this will be the last bit of file manipulation I need to do. I have a file which is formatted as... (4 Replies)
Discussion started by: crunchgargoyle
4 Replies

2. Shell Programming and Scripting

Help awk/sed: putting a space after numbers:to separate number and characters.

Hi Experts, How to sepearate the list digit with letters : with a space from where the letters begins, or other words from where the digits ended. file 52087mo(enbatl) 52049mo(enbatl) 52085mo(enbatl) 25051mo(enbatl) The output should be looks like: 52087 mo(enbatl) 52049... (10 Replies)
Discussion started by: rveri
10 Replies

3. Shell Programming and Scripting

Separate two numbers

I would like to separate number by space so that 121231212 222111212 would be 1 2 1 2 3 1 2 1 2 2 2 2 1 1 1 2 1 2 Thanks! (2 Replies)
Discussion started by: johnkim0806
2 Replies

4. Shell Programming and Scripting

How to split a data file into separate files with the file names depending upon a column's value?

Hi, I have a data file xyz.dat similar to the one given below, 2345|98|809||x|969|0 2345|98|809||y|0|537 2345|97|809||x|544|0 2345|97|809||y|0|651 9685|98|809||x|321|0 9685|98|809||y|0|357 9685|98|709||x|687|0 9685|98|709||y|0|234 2315|98|809||x|564|0 2315|98|809||y|0|537... (2 Replies)
Discussion started by: nithins007
2 Replies

5. Shell Programming and Scripting

the smallest number from 90% of highest numbers from all numbers in file

Hello All, I am having problem to find what is the smallest number from 90% of highest numbers from all numbers in file. I am having file with thousands of lines and hundreds of columns. I am familiar mainly with bash but I am open to whatever suggestion witch will lead to the solutions. If I... (11 Replies)
Discussion started by: Apfik
11 Replies

6. Shell Programming and Scripting

separate two numbers separated by :

Hi all i have a data of this form 10.12.4.22:138 10.12.2.50:137 10.20.2.24:1027 10.12.2.44:138 10.12.2.44:137 10.0.4.38:58871 10.13.3.19:138 i need to separate the number out which is at the last and after ':' Please help me out Thanx in advance (5 Replies)
Discussion started by: vaibhavkorde
5 Replies

7. UNIX for Dummies Questions & Answers

creating separate directories according to file extension and keeping file in different directory as

unix program to which a directory name will be passed as parameter. This directory will contain files with various extensions. This script will create directories with the names of the extention of the files and then put the files in the corresponding folder. All files which do not have any... (2 Replies)
Discussion started by: Deekay.p
2 Replies

8. Shell Programming and Scripting

AWK to separate numbers from logs

Hello friends, Im trying to separate a number from a log, but it seems i need help here awk '/stimated/ {print $5}' mylog.txt gives (1515.45MB). i need pure number part to use in a comparision loop so i want to separate the number part (but only 1515 not 1515.45 ) awk '/stimated/... (6 Replies)
Discussion started by: EAGL€
6 Replies

9. Shell Programming and Scripting

read numbers from file and output which numbers belongs to which range

Howdy experts, We have some ranges of number which belongs to particual group as below. GroupNo StartRange EndRange Group0125 935300 935399 Group2006 935400 935476 937430 937459 Group0324 935477 935549 ... (6 Replies)
Discussion started by: thepurple
6 Replies

10. UNIX for Dummies Questions & Answers

how to separate numbers and words from a file using shell scripts

Hi, How to separate numbers and words(with full alphabets) in a particular file and store it in two different files. Please help me out for this.Using shell scripting. :confused::confused: (1 Reply)
Discussion started by: kamakshi s
1 Replies
Login or Register to Ask a Question