1

I have a string in shell script which is

list="apr/2018-06-24 17_10_39/2018-06-24 17_10_39.html 

 apr/2018-06-24 17_10_39/access_log.zip

 apr/2018-06-25 17_12_48/2018-06-25 17_12_48.html

 apr/2018-06-25 17_12_48/access_log.zip

 apr/2018-06-26 17_13_36/2018-06-26 17_13_36.html

  DS_BLS_731.dat

 DS_BLS_732.dat

 DS_BLS_733.dat

 apr/2018-06-26 17_13_37/ DS_BLS_739.dat

 apr/2018-06-26 17_13_38/ DS_BLS_738.dat

 apr/2018-06-26 17_13_39/ DS_BLS_737.dat"

I need to find DS_BLS_max(sequence number).dat

here DS_BLS_max(sequence number).dat=DS_BLS_739.dat

kgbook
  • 388
  • 4
  • 16

1 Answers1

0
grep -o 'DS_BLS_[0-9]*\.dat' <<< "$list" | sort -V | tail -n 1

Output:

DS_BLS_739.dat

See: The Stack Overflow Regular Expressions FAQ

Cyrus
  • 84,225
  • 14
  • 89
  • 153
  • however note that `11 < 2 < 3` in lexical sorting. So if you have `DS_BLS_1739.dat` you won't get it as the max. – karakfa Aug 24 '18 at 15:12
  • @Cyrus..thanks for reply.The solution is working fine..What if i need to get the whole string. e.g apr/2018-06-26 17_13_37/ DS_BLS_739.dat should return apr/2018-06-26 17_13_37/ DS_BLS_739.dat but its returning only DS_BLS_739.dat – user1643763 Aug 27 '18 at 16:25
  • @user1643763: With a [Schwartzian transform](https://en.wikipedia.org/wiki/Schwartzian_transform) and GNU sed: `sed -nE '/DS_BLS_[0-9]*\.dat/{s/.*(DS_BLS_[0-9]*\.dat).*/\1 &/p}' file | sort -V | tail -n 1 | cut -d " " -f 2-` – Cyrus Aug 27 '18 at 17:44