[SOLVED] sequential : how to find the missing numbers within a sequence of files that have sequential numbers attached to them?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
sequential : how to find the missing numbers within a sequence of files that have sequential numbers attached to them?
Counting how many files one has is a simple matter, but when I got a few hundred and some are missing within the total sequence what is a quick way to find out which ones are missing within the sequence of the total and put them in a file listing only the numbers that are not presence within the sequence?
say we have here these numbers, whereas it is to be all numbers within that sequence of 1-19.
we search though the files with the attached numbers and only see these numbers.
1, 2 , 3, 5, 7, 8, 19.
the means to search this and report back would result in this means saying that, 4, 6, 9-18 are missing and spelling out the last sequence in numbered order as well. The whole sequence of missing numbers being printed out or into a file.
the actual pattern being.
Code:
FileName-xxx-xxxxxxx.ext
where the leading zeros are place holders. the sequence pattern for the numbers then is: 001, 002, 003, -- 010, 011, -- 120 etc
the middle three x's being the ones to look at to find the missing numbers within the entire sequence of numbers.
testfor.sh
#!/bin/bash
PREFIX=$1
SUFFIX=$2
for I in $(seq -f "%03g" 0 19) ; do
CHECKFOR=${PREFIX}${I}${SUFFIX}
if [[ ! -f ${CHECKFOR} ]] ; then
echo -ne "${I}, "
fi
done
#!/bin/bash
working_dir=/run/media/userx/250GB/numberedFiles
a=0
while read file
do
f=$file
path=${f%/*}
xfile=${f##*/}
title=${xfile%.*}
ext=${xfile##*.}
t
#get numbers off of files
numbers=${title#*-}
numbers=${numbers%%-*}
#remove all leading zero's
numbers=$(echo $numbers | sed 's/^0*//')
# make sure they are actual digits
if [[ $numbers =~ ^-?[0-9]+$ ]] ; then NumberedArray[$a]=$numbers ; ((a++)) ; fi
done< <(find "$working_dir" -type f )
# put them in order of 1 - 270
sortedNums=( $( printf "%s\n" "${NumberedArray[@]}" | sort -n ) )
# print them into a file
for (( b= 0 ; $b < "${#sortedNums[@]}" ; b++ ))
{
echo "${sortedNums[$b]}" >> Numbers
}
#not working like I need
#print out missing numbers
#awk '$1!=p+1{print p+1"-"$1-1}{p=$1}' Numbers
awk '$1!=p+1{print p+1}{p=$1}' Numbers
testfor.sh
#!/bin/bash
PREFIX=$1
SUFFIX=$2
for I in $(seq -f "%03g" 0 19) ; do
CHECKFOR=${PREFIX}${I}${SUFFIX}
if [[ ! -f ${CHECKFOR} ]] ; then
echo -ne "${I}, "
fi
done
Then run it in the folder with:
Code:
./testfor.sh /path/to/folder/FileName- -*.ext
that is interesting, a lot shorter than what I've got.
I might have to play with it to get just the numbers off the file?
can it print out the missing numbers in sequence?
if range is between 1 - 100 and missing is 4 - 10, 44, 85 then it prints
4 5 6 7 8 9 10 44 85 NOT 4-10 and whatever else gets printed.
I couldn't resist. Am interested in Laserbeak's comments and/or version.
Hopefully comments are self-explanatory.
Code:
#!/usr/bin/perl
## ^^ set to location of your perl
$working_dir="/run/media/userx/250GB/NumberedFiles";
##~ $working_dir="."; # testing
$max = $ARGV[0]; ## get max number from the command line
$max++;
## get list of file name in array
@files=`find "$working_dir" -type f`;
## remove leading and trailing parts
foreach $file (@files) {
$file =~ s/^.*?-//; #remove from beginning to first hyphen
$file =~ s/-.*$//; #remove from second hyphen to end
$existnums[$file]=$file; #save in array
}
## populate array with all the numbers: 1-input value
for ($i = 1; $i < $max; $i++) {
$allnums[$i] = $i;
}
## remove existing numbers from full list
foreach $nbr (@existnums) {
$allnums[$nbr] = 0
}
## print out the remaining (i.e. missing) numbers
## note, no sorting required because the allnums array is populated in sequence
foreach $nbr (@allnums) {
if ($allnums[$nbr] ne 0) { ## only print the entries that are not -0-
print "$allnums[$nbr] "; ## or
##~ print "$allnums[$nbr]\n"; ## to do one per line
}
}
print "\n"; ## when printing all on one line.
Run with /path/to/thisscript.pl maxvalue > savefile
or /path/to/thisscript.pl maxvalue to see on STDOUT
Last edited by scasey; 07-07-2017 at 04:34 PM.
Reason: Missing ^ in first regex -- added in red
Hmm. Me too. let me see what I broke...
Oh. You get nothing if you don't supply a max value on the command line -- there should probably be a test for that:
Code:
./perl-find-missing-numbers.pl 270
(or whatever the current name of the script is...)
(your #! must be right, or you'd have got errors)
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.