Home > Blockchain >  Collect only numbers in a file's extension
Collect only numbers in a file's extension

Time:03-18

I need some help, I'm creating a script with the purpose of going through a text file line by line and validating it with the images of a folder. My doubt is: when I search for images, I only want the number not the extension.

find /mnt/62-PASTA/01.GERAL/ -mindepth 2 | head -19 | cut -d/ -f6

I get:

111066.jpg
88008538.jpg
11241.jpg
88008563.jpg
116071.PNG

But I want

111066
88008538
11241
88008563
116071

Any help?

CodePudding user response:

A really simple way given the examples shown would be to use cut again to split on .:

find /mnt/62-PASTA/01.GERAL/ -mindepth 2 | head -19 | cut -d/ -f6 | cut -d'.' -f1

CodePudding user response:

What we can do here is use another cut command.

cut -d . 

Now this will give me strings separated by . as delimiter. Then we can grab all but last part as below.

cut -d . -f 1

I think this should work.Check below link for additional details.

CodePudding user response:

With pure shell solution, try following solution. Simple explanation would be, using a for loop to loop through .jpg, .PNG format files in your current directory. Then in main loop, using bash's capability to perform substitution and substitute everything apart from digits with NULL in file's name which will give only digits from file's names.

Running code from directory/mnt/62-PASTA/01.GERAL/:

for file in *.jpg *.PNG;
do
   echo "${file//[^0-9]/}"
done


OR with full path(/mnt/62-PASTA/01.GERAL/) to run from any other path try following code:

for file in /mnt/62-PASTA/01.GERAL/*.jpg  /mnt/62-PASTA/01.GERAL/*.PNG;
do
   file1="${file##*/}"      ##Removing values till / to get only filename.
   echo "${file1//[^0-9]/}" ##Removing everything apart from digits in filename.
done

Output will be as follows:

111066
11241
88008538
88008563
116071
  • Related