Friday, February 18, 2022

[SOLVED] Find the highest number inside a file

Issue

I am trying to find the highest number inside a string from a file. For example, in the file password.txt we have:

 jaime:45:/home/jaime:/bin/bash
 sofia:113:/home/sofia:/bin/bash
 marta:2015:/home/marta:/bin/bash
 pedro:2024:/home/pedro:/bin/bash 

So the highest number should be 2024 and we have to save it into a variable:-

number=2024

I've tried several things with grep, awk, sed or even with sort, but without any solution.


Solution

I suggest:

number=$(cut -d: -f 2 file | sort -n | tail -n 1)


Answered By - Cyrus
Answer Checked By - Terry (WPSolving Volunteer)