1
highestVersion() {
    for TEST_PATH in `ls -a "$/TEMP"/*.flg`
    do
        echo " you are here"
        TEST_FILE=`basename "$TEST_PATH"`
        echo "$TEST_FILE"
    done
}

Below is the file TEST_FILE. In this case for example "RMG" is a book name having 4 version. How can I pick the file with latest version among these set of files and store it in a variable? How can I iterate through this, so that at the end, the variable should contain only the unique filename with highest version number?

ABC_EFG_FLOOL_DR3GCTEU_19951223_HANSHAKE_V03.flg
ABC_EFG_FLOOL_DR3GCTPC_19951223_HANSHAKE_V03.flg
ABC_RFTY_PICK_AMTY_19951223_HANSHAKE_V03.flg
ABC_JANE_PICK_AMTY_19951223_HANSHAKE_V02.flg
ABC_XYZ_RMG_19951223_HANSHAKE_V01.flg
ABC_XYZ_RMG_19951223_HANSHAKE_V02.flg
ABC_XYZ_RMG_19951223_HANSHAKE_V03.flg
ABC_XYZ_RMG_19951223_HANSHAKE_V11.flg
ABC_XYZ_ALG0_19951223_HANSHAKE_V02.flg
ABC_XYZ_SGFXMM_19951223_HANSHAKE_V03.flg
ABC_XYZ_STRIP_FULLREVAL_YU_1234_19951223_HANSHAKE_V02.flg
ABC_XYZ_STRIP_FULLREVAL_YU_1234_19951223_HANSHAKE_V03.flg
1
  • Use should ask your question in proper way. It is difficult to understand your problem. Please use appropriate keywords and title to get it correct. Commented Dec 22, 2015 at 10:52

2 Answers 2

4

Since the version order is the same as the lexical order in your case, with a POSIX shell:

set -- ABC_XYZ_RMG_19951223_HANSHAKE_V*.flg
# now $1, $2... contain the files from the lowest version to highest.

shift "$(($# - 1))"
highest_version=$1

# or without shift:
eval "highest_version=\${$#}"

Or with zsh or recent versions of bash:

rmgs=(ABC_XYZ_RMG_19951223_HANSHAKE_V*.flg)
highest_version=${rmgs[-1]}

To do it with every file in the current directory (again with zsh or recent versions of bash (4.0 or above)):

highest_versions() (
  cd -P -- "$TEMP" || exit
  typeset -A a
  for f in *V??.flg; do
    a[${f%V??.flg}]=$f
  done
  printf '%s\n' "${a[@]}"
)

On your files, that gives:

ABC_XYZ_STRIP_FULLREVAL_YU_1234_19951223_HANSHAKE_V03.flg
ABC_XYZ_RMG_19951223_HANSHAKE_V11.flg
ABC_JANE_PICK_AMTY_19951223_HANSHAKE_V02.flg
ABC_XYZ_ALG0_19951223_HANSHAKE_V02.flg
ABC_XYZ_SGFXMM_19951223_HANSHAKE_V03.flg
ABC_RFTY_PICK_AMTY_19951223_HANSHAKE_V03.flg
ABC_EFG_FLOOL_DR3GCTEU_19951223_HANSHAKE_V03.flg
ABC_EFG_FLOOL_DR3GCTPC_19951223_HANSHAKE_V03.flg

With shells that don't support associative arrays, you can do the same by invoking a tool that does like awk:

highest_versions() (
  cd -P -- "$TEMP" || exit
  awk 'BEGIN{
    for (i = 1; i < ARGC; i++) {
      key = val = ARGV[i]
      sub(/V..\.flg/, "", key)
      a[key] = val
    }
    for (i in a) print a[i]
    exit}' *V??.flg
)
4
  • But I am using 3.2.25(1)-release bash version..what the modification needs to be done.can you help me? Commented Dec 23, 2015 at 13:51
  • Do it work fine with 3.2.25(1)-release bash version.Can you suggest the changes I need to make..Thank you in advance:) Commented Dec 23, 2015 at 13:57
  • Because I am getting error-- syntax error near unexpected token `)'. Commented Dec 23, 2015 at 14:00
  • 1
    @sunilkumarjn, 3.2 is from 2006, I wouldn't call that recent. See the edit for a portable solution. Commented Dec 23, 2015 at 15:25
0

I like perl so I'll suggest a perlish solution:

#!/usr/bin/env perl
use strict;
use warnings;
my %highest_version_of;

for ( glob ( "*.flg" ) ) { 
    my ( $filename, $version ) = m/^(\w+)_V(\d+).flg/; 
    if ( $highest_version_of{$filename} //= 0 < $version ) { $highest_version_of{$filename} = $version }; 
}
foreach my $filename ( sort keys %highest_version_of ) {
    print "${filename}_$highest_version_of{$filename}.flg\n";
}

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.