This script breaks up the cvs list into three columns. we are focusing on the "name" column. I want to discover the name that has the most characters. Once I find the name with the most characters, I want to assign that to a variable.
#!/bin/bash
for i in $(cat homeaway.txt )
do
echo $i | while IFS=, read -r area name host
do
maxLength=0
length=${#name}
if [ $length -gt $maxLength ] ; then
maxLength=$length
else
:
fi
printf "%s\n" $maxLength
done
done
The script says - in English - If the length is greater than maxlength, set length to maxLength, if not, do nothing. The area string with the most characters in it is "script_name_12345678999999" which has 26. When the script reads through all the characters, $maxLength should return 26.
__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
Once the script reaches the area value with 26 characters in it, it should stop assigning anything to $maxLength. Instead it returns a list of each strings length, and I have no idea how the zero gets in here
[email protected] $ ./length_test.sh
17
0 ### how does the zero get in here ?
15
13
15
18
26 ###script_name_12345678999999
16
19
14
21
my_command | sort -n | tail -1
Sort the output of the command numeric and ascending. Get the last element in the resulting list.
On GNU/Linux, you can also do this in one shot. If the file data has your records:
cut --delimiter=, --fields=2 < data | wc --max-line-length
In English:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With