hrexen hrexen - 6 months ago 32
Bash Question

Converting ls output to an integer

I would like to extract a set of 2 digit numbers from the

ls
output and be able to get the biggest and smallest numbers from it, for later usage.

I am doing this:
ls 14_data_*.log | egrep -o '[0-9]{2,2}' | head -1}
but the output is only sometimes in an ascending order and
head
,
tail
of expectantly return wrong values. I tried converting it to an integer but hopeless.

What is returned by
ls
and how can it be handled?

Answer

ls returns a list of files and directories in the current directory.

I don't know what you are upto but here is a solution to your problem using sort:

Descending order:

ls 14_data_*.log | egrep -o '[0-9]{2}' | sort -nr

Ascending order:

ls 14_data_*.log | egrep -o '[0-9]{2}' | sort -n
Comments