-1

How can I find file with highest alphabet character in the file's extension? Example of files my application creates:

$ find . -name 'L_*.[J-Z]'
L_58420.K
L_58420.J
L_58420.M
L_46657.J
L_58420.N
L_58420.P
L_46657.N
L_58420.Q
L_46657.K
L_58420.O
L_46657.O
L_46657.L
L_46657.M
L_58420.L

and I'd like to have returned :

L_58420.Q
L_46657.O

Higher alphabet character is created only if file with previous character already exists, so it's possible to search/sort by date too.

user unknown
  • 35,537
  • 11
  • 75
  • 121
mauek unak
  • 702
  • 2
  • 11
  • 28

3 Answers3

1
echo "L_58420.K
L_58420.J
L_58420.M
L_46657.J
L_58420.N
L_58420.P
L_46657.N
L_58420.Q
L_46657.K
L_58420.O
L_46657.O
L_46657.L
L_46657.M
L_58420.L" | sed 's/[_.]/ /g' | sort -r -k 2 -k 3 |  while read L no c ; do if [[ "$no" != "$last" ]]; then echo L_$no.$c; last=$no; fi; done; 
L_58420.Q
L_46657.O

Sed splits the String at _ and . into pieces, so sort can sort by key 2 (number) and 3 (trailing char). A while loop reads the constant L (ignores it), a no and the c. Prints, if it is a new $no and keeps it as the new $last. In the output, we inject _ and . back again.

old solution before clarification in comment:

Pipe it through rev | sort -r | rev:

echo "L_58420.K
L_58420.J
L_58420.M
L_46657.J
L_58420.N
L_58420.P
L_46657.N
L_58420.Q
L_46657.K
L_58420.O
L_46657.O
L_46657.L
L_46657.M
L_58420.L" | rev | sort -r | rev 
L_58420.Q
L_58420.P
L_46657.O
L_58420.O
L_46657.N
L_58420.N
L_46657.M
L_58420.M
L_46657.L
L_58420.L
L_46657.K
L_58420.K
L_46657.J
L_58420.J

The rev reverses a line, sort -r sorts reverse (descending) and again rev brings the strings back into the right order.

To reduce the output to two lines, add a pipe | head -n 2 to the end. You can append the whole pipeline to your find command, but I didn't like to generate all that file names in a test environment. :)

Community
  • 1
  • 1
user unknown
  • 35,537
  • 11
  • 75
  • 121
  • it's almost perfectly working, only that head -n 2 return first (highest) 2 characters: 1. - L_58420.Q 2. - L_58420.P 3. - L_46657.O 4. - L_58420.O and from that I need first and third row – mauek unak Mar 19 '18 at 22:23
  • You didn't say that you need the first and third row. Is there some implicit assumption? Last character by maximum, then number by minimum? – user unknown Mar 19 '18 at 22:30
  • I'd like to have returned unique name with highest alphabet character as file type – mauek unak Mar 19 '18 at 22:31
  • So for each number, the maximum file extension? (I stick to my belief, that all those files have the same file type, regardless of their extension.) You should have used the plural form in your question - it's not that clear. It looks as if that is only the beginning of the reordering. – user unknown Mar 19 '18 at 22:40
  • yes, for each number highest alphabet letter. and yes, they are all same files – mauek unak Mar 19 '18 at 22:42
1

Simple sorting:

find . -name 'L_*.[J-Z]' -printf "%f\n" | sort -t'.' -k2,2r
RomanPerekhrest
  • 88,541
  • 4
  • 65
  • 105
  • works the same as answer from @user unknown, problem is that it returns all hits from highest alphabet character down, Q-O-N ... and I need only (highest) .Q file – mauek unak Mar 19 '18 at 22:30
1

Sort on second field and get unique filenames:

find ... | sort -t"." -k2,1 -r | 
   awk -F. '$1!=lastname {lastname=$1; print $0; i++; if (i==2) exit}'

When all files have the same filelength, you can use

find ... | sort -t"." -k2,1 -r | uniq -w7
Walter A
  • 19,067
  • 2
  • 23
  • 43