Reputation: 2635
I am trying to get the longest_length set to the highest number.
#!/bin/bash
for i in $(cat list_of_aways)
do
echo $i | while IFS=, read -r area name host
do
printf "%s\n" $name
sleep 1
longest_length=${#name}
printf "%s\n" "$longest_length"
done
done
This is the data. The 9999999 is the longest string - I want to set that variable to the arrays value because it is the longest.
__DATA__
HOME,script_name_12345,USAhost.com
AWAY,script_name_123,USAhost.com
HOME,script_name_1,EUROhost.com
AWAY,script_name_123,USAhost.com
HOME,script_name_123456,EUROhost.com
AWAY,script_name_12345678999999,USAhost.com
HOME,script_name_1234,USAhost.com
AWAY,script_name_1234578,USAhost.com
HOME,script_name_12,EUROhost.com
AWAY,script_name_123456789,USAhost.com
Upvotes: 2
Views: 45
Reputation: 334
You can actually do it with an awk command:
awk -F '_|,' 'NR>1 {if ( length($4) > length(max) ) { max=$4 }} END { print max }' INPUTFILE
_|,
= We are using 2 delimiters _
and ,
if ( length($4) > length(max) ) { max=$4 }
= var max
gets is compared with current 4th element in each line, and max is chosen.
print max
= The max value is printed out.
If you want to set the result into a var called longest_length
:
longest_length = `awk -F '_|,' 'NR>1 {if ( length($4) > length(max) ) { max=$4 }} END { print max }' INPUTFILE`
Upvotes: 1