3

I need a shell script to find a string in file like the following one: FileName_1.00_r0102.tar.gz And then pick the highest value from multiple occurrences.

I am interested in "1.00" part of the file name. I am able to get this part separately in the UNIX shell using the commands:

find /directory/*.tar.gz | cut -f2 -d'_' | cut -f1 -d'.'
1
2
3
1
find /directory/*.tar.gz | cut -f2 -d'_' | cut -f2 -d'.'
00
02
05
00

The problem is there are multiple files with this string:

FileName_1.01_r0102.tar.gz

FileName_2.02_r0102.tar.gz

FileName_3.05_r0102.tar.gz

FileName_1.00_r0102.tar.gz

I need to pick the file with FileName_("highest value")_r0102.tar.gz

But since I am new to shell scripting I am not able to figure out how to handle these multiple instances in script.

The script which I came up with just for the integer part is as follows:

#!/bin/bash
for file in /directory/*
file_version = find /directory/*.tar.gz | cut -f2 -d'_' | cut -f1 -d'.'
done
OUTPUT: file_version:command not found

Kindly help. Thanks!

EmbeddedManiac
  • 189
  • 1
  • 12
  • 2
    This is not an answer, just an attempt to identify syntax errors, so that you can avoid them next time: 1) you need a `do` in a for loop: `for .... ;do .... ;done` 2) when you set a variable you cannot have spaces around the equals sign: `file_version=5`, *not* `file_version = 5` 3) to set a variable to contain the output of a command, you need [command substitution](https://www.gnu.org/software/bash/manual/html_node/Command-Substitution.html): `file_version=$(command)` 4) not a syntax error, but it is a good idea to quote the RHS of a variable assignment: `file_version="$(command)"`. – NickD Jun 16 '17 at 21:43
  • 1
    Good advice above, but don't make us syntax check your scripts for you, learn to use http://shellcheck.net . Do you know about the `head` and `tail` cmds. That should help if you can force your output to be sorted. Good luck. – shellter Jun 17 '17 at 03:30
  • Thanks a lot, @Nick! – EmbeddedManiac Jun 19 '17 at 14:03
  • @shellter This was my first time writing a script. shellcheck.net is really useful. Thanks!! – EmbeddedManiac Jun 19 '17 at 14:06

4 Answers4

2

You could try the following which finds all the matching files, sorts the filenames, takes the last in that list, and then extracts the version from the filename.

#!/bin/bash
file_version=$(find ./directory -name "FileName*r0102.tar.gz" | sort | tail -n1 | sed -r 's/.*_(.+)_.*/\1/g')
echo ${file_version}
Jonathan
  • 1,382
  • 1
  • 13
  • 13
2

If you just want the latest version number:

cd /path/to/files
printf '%s\n' *r0102.tar.gz | cut -d_ -f2 |  sort -n -t. -k1,2 |tail -n1

If you want the file name:

cd /path/to/files
lastest=$(printf '%s\n' *r0102.tar.gz | cut -d_ -f2 |  sort -n -t. -k1,2 |tail -n1)
printf '%s\n' *${lastest}_r0102.tar.gz
fd0
  • 289
  • 7
  • 10
1

It's unnecessary to parse the filename's version number prior to finding the actual filename. Use GNU ls's -v (natural sort of (version) numbers within text) option:

ls -v FileName_[0-9.]*_r0102.tar.gz | tail -1
agc
  • 7,973
  • 2
  • 29
  • 50
1

I have tried and thats worth working below script line, that You need.

echo `ls ./*.tar.gz | sort | sed  -n /[0-9]\.[0-9][0-9]/p|tail -n 1`;
Harini
  • 551
  • 5
  • 18