Skip to main content
added 414 characters in body
Source Link
Stéphane Chazelas
  • 584.6k
  • 96
  • 1.1k
  • 1.7k

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
)

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):

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

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
)
added 203 characters in body
Source Link
Stéphane Chazelas
  • 584.6k
  • 96
  • 1.1k
  • 1.7k

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):

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

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]}

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):

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
Source Link
Stéphane Chazelas
  • 584.6k
  • 96
  • 1.1k
  • 1.7k

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]}