0

I am trying to find te longest word in a given file. Before I check the lengtgh of each word I need to remove all of the following tokens {,.:} that may be attached (once or more) to the word. so for example, for this text:

:,cat dog, encyclopedia; remove:.,

i need the result:

cat dog encyclopedia remove

I am trying this, but I get a "command not found":

longest=0
for word in $(<$1)
do
    #new_word = $(echo "${word//[.,:]/}")
    new_word = "${word//[.,:]/}"
    len=${#new_word}

    if (( len > longest ))
    then
        longest=$len
        longword=$new_word
    fi
done
echo The longest word is $longword and its length is $longest.

thank you.

avital.ko
  • 43
  • 4

1 Answers1

0

Your use of parameter expansion replacement pattern is correct.

The problem is that there must not be any whitespace around = while declaring variables in bash (any shell in general).

So, the following should work:

new_word="${word//[.,:]/}"

As an aside, use a while read ... construct to loop over the lines in a file, using for is pretty fragile.

heemayl
  • 39,294
  • 7
  • 70
  • 76