Reputation: 21
I have list of files in directory,ending with numeric. As below:
play_football_3
play_football_4
play_football_5
play_football_15
play_football_59
I'm able to extract the last numeric digit of above files. as
echo "play_football_5" | cut -f3 -d"_"
Now Here i'm trying to list all files which have higher version then play_football_5.
Expected Output:
play_football_15
play_football_59
Upvotes: 2
Views: 89
Reputation: 8711
Using Perl one-liner
> ll
total 0
-rw-r--r-- 1 xxxxxx devlgrp 0 Oct 30 14:41 play_football_5
-rw-r--r-- 1 xxxxxx devlgrp 0 Oct 30 14:41 play_football_4
-rw-r--r-- 1 xxxxxx devlgrp 0 Oct 30 14:41 play_football_3
-rw-r--r-- 1 xxxxxx devlgrp 0 Oct 30 14:41 play_football_15
-rw-r--r-- 1 xxxxxx devlgrp 0 Oct 30 14:41 play_football_59
> perl -ne ' BEGIN { @files=glob("play*");foreach(@files){ ($file=$_)=~s/.*_(\d+)/\1/g; print "$_\n" if $file > 5 } exit }'
play_football_15
play_football_59
>
Upvotes: 0
Reputation: 565
You can also keep your original idea doing something like this:
for file in $(ls | cut -d_ -f3)
do
if [[ "$file" -gt 5 ]]
then
echo "play_football_$file"
fi
done
If you do that while in the directory that has your files you will get:
play_football_15
play_football_59
Upvotes: 0
Reputation: 207445
If you have bash
2.02-alpha1, or newer, you can turn on "extended globbing" and look for files starting play_football_
and not ending with the digits 0-5 like this:
shopt -s extglob
ls play_football_!([0-5])
Here is a reference to start learning more about it.
Upvotes: 3
Reputation: 13249
You can use awk
for this:
printf "%s\n" play_football_* | awk -F_ '$3>5'
printf
will list all files in the current directory start with play_football_
and awk
filters the files with the number greater than 5
Upvotes: 3
Reputation: 626758
You may use
awk -F'_' '$3 > 5'
See the online awk
demo
With -F'_'
, you set the delimiter to an underscore and with '$3 > 5'
, only those lines (records) are printed where the third field is greater than 5
.
Upvotes: 0