Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

extract number from file name

Status
Not open for further replies.

liondari

Technical User
Oct 4, 2004
7
0
0
GR
Hello,

I would like to extract an int number from a file name. I have
hundreds of files with the following names:
Qhull_1000_001.xyz ... Qhull_15000_150.xyz.
Now, I need the first number to be extracted and saved into a file. The second number I use for steering a for loop.
Thanks in advance
 
does this help?

ls | cut -d_ -f2 > /path/to/numbersfile


HTH,

p5wizard
 
Here are some more thoughts...
Also, be more specific.

Code:
for fn in Qhull_*_*.xyz
do
  CT=$(expr "$fn" : ".*_0*\([0-9]*\)\..*")
  echo $fn $(expr "$fn" : ".*_\(.*\)_.*") $CT >> /path/to/numberedfile
  # additional for loop here, if desired
done

Cheers,
ND [smile]

[small]bigoldbulldog AT hotmail[/small]
 
A Korn shell variable editing solution...
Code:
#!/bin/ksh

for FILE in Qhull_*_*.xyz
do
    NUM1=${FILE#Qhull_}   # Remove the front 'Qhull_'
    NUM1=${NUM1%%_*}      # Remove the back '_*'

    NUM2=${FILE##*_}      # Remove the front '*_'
    NUM2=${NUM2%.*}       # Remove the back '.*'

    print "1st number: ${NUM1}"
    print "2nd number: ${NUM2}"

    print ${NUM1} >> /path-to/numbers.file
done
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top