How to find the shortest word on a text file

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
-1
down vote

favorite












So i have all these words in a text file:



dfasdfasdf
adsgad
fghjast
hdasfh
adfhadfn


And i have to display the number of chars of the smallest word without using the 'awk' and 'sed' commands...
I have tried all variants of wc but it works with the full text file and not only line per line.










share|improve this question









New contributor




TOY is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.















  • 1




    What do you mean by smallest? The shortest? Can you use sort?
    – choroba
    14 mins ago










  • Yes, the shortest one. I have used sort but it only sorts me the words by alphabetical order.
    – TOY
    12 mins ago










  • mapfile -t arr < yourtextfile; for f in "$arr[@]"; do printf '%sn' "$#f"; done | sort -n |head -n1 or using the read as follows: while read -r line; do printf '%sn' "$#line"; done < fff | sort -n | head -n1
    – Valentin Bajrami
    7 mins ago















up vote
-1
down vote

favorite












So i have all these words in a text file:



dfasdfasdf
adsgad
fghjast
hdasfh
adfhadfn


And i have to display the number of chars of the smallest word without using the 'awk' and 'sed' commands...
I have tried all variants of wc but it works with the full text file and not only line per line.










share|improve this question









New contributor




TOY is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.















  • 1




    What do you mean by smallest? The shortest? Can you use sort?
    – choroba
    14 mins ago










  • Yes, the shortest one. I have used sort but it only sorts me the words by alphabetical order.
    – TOY
    12 mins ago










  • mapfile -t arr < yourtextfile; for f in "$arr[@]"; do printf '%sn' "$#f"; done | sort -n |head -n1 or using the read as follows: while read -r line; do printf '%sn' "$#line"; done < fff | sort -n | head -n1
    – Valentin Bajrami
    7 mins ago













up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











So i have all these words in a text file:



dfasdfasdf
adsgad
fghjast
hdasfh
adfhadfn


And i have to display the number of chars of the smallest word without using the 'awk' and 'sed' commands...
I have tried all variants of wc but it works with the full text file and not only line per line.










share|improve this question









New contributor




TOY is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











So i have all these words in a text file:



dfasdfasdf
adsgad
fghjast
hdasfh
adfhadfn


And i have to display the number of chars of the smallest word without using the 'awk' and 'sed' commands...
I have tried all variants of wc but it works with the full text file and not only line per line.







shell awk sed grep wc






share|improve this question









New contributor




TOY is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




TOY is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 11 mins ago





















New contributor




TOY is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 15 mins ago









TOY

11




11




New contributor




TOY is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





TOY is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






TOY is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







  • 1




    What do you mean by smallest? The shortest? Can you use sort?
    – choroba
    14 mins ago










  • Yes, the shortest one. I have used sort but it only sorts me the words by alphabetical order.
    – TOY
    12 mins ago










  • mapfile -t arr < yourtextfile; for f in "$arr[@]"; do printf '%sn' "$#f"; done | sort -n |head -n1 or using the read as follows: while read -r line; do printf '%sn' "$#line"; done < fff | sort -n | head -n1
    – Valentin Bajrami
    7 mins ago













  • 1




    What do you mean by smallest? The shortest? Can you use sort?
    – choroba
    14 mins ago










  • Yes, the shortest one. I have used sort but it only sorts me the words by alphabetical order.
    – TOY
    12 mins ago










  • mapfile -t arr < yourtextfile; for f in "$arr[@]"; do printf '%sn' "$#f"; done | sort -n |head -n1 or using the read as follows: while read -r line; do printf '%sn' "$#line"; done < fff | sort -n | head -n1
    – Valentin Bajrami
    7 mins ago








1




1




What do you mean by smallest? The shortest? Can you use sort?
– choroba
14 mins ago




What do you mean by smallest? The shortest? Can you use sort?
– choroba
14 mins ago












Yes, the shortest one. I have used sort but it only sorts me the words by alphabetical order.
– TOY
12 mins ago




Yes, the shortest one. I have used sort but it only sorts me the words by alphabetical order.
– TOY
12 mins ago












mapfile -t arr < yourtextfile; for f in "$arr[@]"; do printf '%sn' "$#f"; done | sort -n |head -n1 or using the read as follows: while read -r line; do printf '%sn' "$#line"; done < fff | sort -n | head -n1
– Valentin Bajrami
7 mins ago





mapfile -t arr < yourtextfile; for f in "$arr[@]"; do printf '%sn' "$#f"; done | sort -n |head -n1 or using the read as follows: while read -r line; do printf '%sn' "$#line"; done < fff | sort -n | head -n1
– Valentin Bajrami
7 mins ago











1 Answer
1






active

oldest

votes

















up vote
0
down vote













Using parameter expansion, you can get the length of a variable contents:



$#variable


Just read the file line by line and remember the shortest word:



while read word ; do
: $shortest:=$word
if [ $#word -lt $#shortest ] ; then
shortest=$word
fi
done < "$1"
echo "$shortest"




share




















    Your Answer







    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "106"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );






    TOY is a new contributor. Be nice, and check out our Code of Conduct.









     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f478733%2fhow-to-find-the-shortest-word-on-a-text-file%23new-answer', 'question_page');

    );

    Post as a guest






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    Using parameter expansion, you can get the length of a variable contents:



    $#variable


    Just read the file line by line and remember the shortest word:



    while read word ; do
    : $shortest:=$word
    if [ $#word -lt $#shortest ] ; then
    shortest=$word
    fi
    done < "$1"
    echo "$shortest"




    share
























      up vote
      0
      down vote













      Using parameter expansion, you can get the length of a variable contents:



      $#variable


      Just read the file line by line and remember the shortest word:



      while read word ; do
      : $shortest:=$word
      if [ $#word -lt $#shortest ] ; then
      shortest=$word
      fi
      done < "$1"
      echo "$shortest"




      share






















        up vote
        0
        down vote










        up vote
        0
        down vote









        Using parameter expansion, you can get the length of a variable contents:



        $#variable


        Just read the file line by line and remember the shortest word:



        while read word ; do
        : $shortest:=$word
        if [ $#word -lt $#shortest ] ; then
        shortest=$word
        fi
        done < "$1"
        echo "$shortest"




        share












        Using parameter expansion, you can get the length of a variable contents:



        $#variable


        Just read the file line by line and remember the shortest word:



        while read word ; do
        : $shortest:=$word
        if [ $#word -lt $#shortest ] ; then
        shortest=$word
        fi
        done < "$1"
        echo "$shortest"





        share











        share


        share










        answered 6 secs ago









        choroba

        25.3k44269




        25.3k44269




















            TOY is a new contributor. Be nice, and check out our Code of Conduct.









             

            draft saved


            draft discarded


















            TOY is a new contributor. Be nice, and check out our Code of Conduct.












            TOY is a new contributor. Be nice, and check out our Code of Conduct.











            TOY is a new contributor. Be nice, and check out our Code of Conduct.













             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f478733%2fhow-to-find-the-shortest-word-on-a-text-file%23new-answer', 'question_page');

            );

            Post as a guest













































































            Popular posts from this blog

            How to check contact read email or not when send email to Individual?

            Bahrain

            Postfix configuration issue with fips on centos 7; mailgun relay