How to find the shortest word on a text file
Clash 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.
shell awk sed grep wc
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.
add a comment |Â
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.
shell awk sed grep wc
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 usesort
?
â 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 theread
as follows:while read -r line; do printf '%sn' "$#line"; done < fff | sort -n | head -n1
â Valentin Bajrami
7 mins ago
add a comment |Â
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.
shell awk sed grep wc
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
shell awk sed grep wc
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.
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 usesort
?
â 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 theread
as follows:while read -r line; do printf '%sn' "$#line"; done < fff | sort -n | head -n1
â Valentin Bajrami
7 mins ago
add a comment |Â
1
What do you mean by smallest? The shortest? Can you usesort
?
â 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 theread
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
add a comment |Â
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"
add a comment |Â
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"
add a comment |Â
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"
add a comment |Â
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"
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"
answered 6 secs ago
choroba
25.3k44269
25.3k44269
add a comment |Â
add a comment |Â
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.
TOY is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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 theread
as follows:while read -r line; do printf '%sn' "$#line"; done < fff | sort -n | head -n1
â Valentin Bajrami
7 mins ago