How to count words in a line

Clash Royale CLAN TAG#URR8PPP
I have a text file called "shoplist.txt" which one have:
drinks water cola fanta
fruit banana orange
And I want to get how many items per line I have. I'm able to extract drinks and fruit with function "cut" but how can I count how many words I have in each line?
My actually code is:
fileLine=`cat file.txt`
#Here I get each line saving it to fileLine
for line in $fileLine; do
echo
((aux++))
done
But this code dosen't work because it save to %fileLine each work (drinks, then water,then cola,...)
How can I get the first line and then count the words on that line?
bash shell-script
add a comment |
I have a text file called "shoplist.txt" which one have:
drinks water cola fanta
fruit banana orange
And I want to get how many items per line I have. I'm able to extract drinks and fruit with function "cut" but how can I count how many words I have in each line?
My actually code is:
fileLine=`cat file.txt`
#Here I get each line saving it to fileLine
for line in $fileLine; do
echo
((aux++))
done
But this code dosen't work because it save to %fileLine each work (drinks, then water,then cola,...)
How can I get the first line and then count the words on that line?
bash shell-script
wc would do the necessary job in a loop.
– Puspharaj Selvaraj
Mar 5 at 5:31
Is the output of theawkscript in the answer what you want to have? If not, what exactly do you want to get?
– Bodo
Mar 5 at 11:09
add a comment |
I have a text file called "shoplist.txt" which one have:
drinks water cola fanta
fruit banana orange
And I want to get how many items per line I have. I'm able to extract drinks and fruit with function "cut" but how can I count how many words I have in each line?
My actually code is:
fileLine=`cat file.txt`
#Here I get each line saving it to fileLine
for line in $fileLine; do
echo
((aux++))
done
But this code dosen't work because it save to %fileLine each work (drinks, then water,then cola,...)
How can I get the first line and then count the words on that line?
bash shell-script
I have a text file called "shoplist.txt" which one have:
drinks water cola fanta
fruit banana orange
And I want to get how many items per line I have. I'm able to extract drinks and fruit with function "cut" but how can I count how many words I have in each line?
My actually code is:
fileLine=`cat file.txt`
#Here I get each line saving it to fileLine
for line in $fileLine; do
echo
((aux++))
done
But this code dosen't work because it save to %fileLine each work (drinks, then water,then cola,...)
How can I get the first line and then count the words on that line?
bash shell-script
bash shell-script
edited Mar 4 at 18:34
Rui F Ribeiro
41.8k1483142
41.8k1483142
asked Mar 4 at 18:26
Multi17Multi17
202
202
wc would do the necessary job in a loop.
– Puspharaj Selvaraj
Mar 5 at 5:31
Is the output of theawkscript in the answer what you want to have? If not, what exactly do you want to get?
– Bodo
Mar 5 at 11:09
add a comment |
wc would do the necessary job in a loop.
– Puspharaj Selvaraj
Mar 5 at 5:31
Is the output of theawkscript in the answer what you want to have? If not, what exactly do you want to get?
– Bodo
Mar 5 at 11:09
wc would do the necessary job in a loop.
– Puspharaj Selvaraj
Mar 5 at 5:31
wc would do the necessary job in a loop.
– Puspharaj Selvaraj
Mar 5 at 5:31
Is the output of the
awk script in the answer what you want to have? If not, what exactly do you want to get?– Bodo
Mar 5 at 11:09
Is the output of the
awk script in the answer what you want to have? If not, what exactly do you want to get?– Bodo
Mar 5 at 11:09
add a comment |
2 Answers
2
active
oldest
votes
If you can use awk, NF is the number of fields in the current line (by default, a field is a word delimited by any amount of whitespace).
Use
awk ' print NF, $0 ' inputfile
With your sample input, this will print
4 drinks water cola fanta
3 fruit banana orange
The more crypticawk '$0=NF" "$0' inputfileis a shorter solution.
– Isaac
Mar 5 at 4:14
@Isaac: Really ? Is shorter better ? In this specific case Bodo's answer is just the answer to OP, ... and [s]he beat us all to it. :-[
– Cbhihe
Mar 5 at 11:05
Thanks everyone for their advices. I didn't know about awk, thanks!
– Multi17
Mar 5 at 17:25
add a comment |
In Bash and wc:
IFS=$'n'
while read line; do
wc -w <<< "$line"
done < file.txt
wc counts lines, words, bytes in files. With a shell loop you can make it count words in a line.
Thanks, I was aware about WC -l for lines but not for words, thanks!
– Multi17
Mar 5 at 17:25
add a comment |
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',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
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
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f504326%2fhow-to-count-words-in-a-line%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
If you can use awk, NF is the number of fields in the current line (by default, a field is a word delimited by any amount of whitespace).
Use
awk ' print NF, $0 ' inputfile
With your sample input, this will print
4 drinks water cola fanta
3 fruit banana orange
The more crypticawk '$0=NF" "$0' inputfileis a shorter solution.
– Isaac
Mar 5 at 4:14
@Isaac: Really ? Is shorter better ? In this specific case Bodo's answer is just the answer to OP, ... and [s]he beat us all to it. :-[
– Cbhihe
Mar 5 at 11:05
Thanks everyone for their advices. I didn't know about awk, thanks!
– Multi17
Mar 5 at 17:25
add a comment |
If you can use awk, NF is the number of fields in the current line (by default, a field is a word delimited by any amount of whitespace).
Use
awk ' print NF, $0 ' inputfile
With your sample input, this will print
4 drinks water cola fanta
3 fruit banana orange
The more crypticawk '$0=NF" "$0' inputfileis a shorter solution.
– Isaac
Mar 5 at 4:14
@Isaac: Really ? Is shorter better ? In this specific case Bodo's answer is just the answer to OP, ... and [s]he beat us all to it. :-[
– Cbhihe
Mar 5 at 11:05
Thanks everyone for their advices. I didn't know about awk, thanks!
– Multi17
Mar 5 at 17:25
add a comment |
If you can use awk, NF is the number of fields in the current line (by default, a field is a word delimited by any amount of whitespace).
Use
awk ' print NF, $0 ' inputfile
With your sample input, this will print
4 drinks water cola fanta
3 fruit banana orange
If you can use awk, NF is the number of fields in the current line (by default, a field is a word delimited by any amount of whitespace).
Use
awk ' print NF, $0 ' inputfile
With your sample input, this will print
4 drinks water cola fanta
3 fruit banana orange
edited Mar 4 at 22:12
Kusalananda♦
139k17259430
139k17259430
answered Mar 4 at 18:31
BodoBodo
2,271618
2,271618
The more crypticawk '$0=NF" "$0' inputfileis a shorter solution.
– Isaac
Mar 5 at 4:14
@Isaac: Really ? Is shorter better ? In this specific case Bodo's answer is just the answer to OP, ... and [s]he beat us all to it. :-[
– Cbhihe
Mar 5 at 11:05
Thanks everyone for their advices. I didn't know about awk, thanks!
– Multi17
Mar 5 at 17:25
add a comment |
The more crypticawk '$0=NF" "$0' inputfileis a shorter solution.
– Isaac
Mar 5 at 4:14
@Isaac: Really ? Is shorter better ? In this specific case Bodo's answer is just the answer to OP, ... and [s]he beat us all to it. :-[
– Cbhihe
Mar 5 at 11:05
Thanks everyone for their advices. I didn't know about awk, thanks!
– Multi17
Mar 5 at 17:25
The more cryptic
awk '$0=NF" "$0' inputfile is a shorter solution.– Isaac
Mar 5 at 4:14
The more cryptic
awk '$0=NF" "$0' inputfile is a shorter solution.– Isaac
Mar 5 at 4:14
@Isaac: Really ? Is shorter better ? In this specific case Bodo's answer is just the answer to OP, ... and [s]he beat us all to it. :-[
– Cbhihe
Mar 5 at 11:05
@Isaac: Really ? Is shorter better ? In this specific case Bodo's answer is just the answer to OP, ... and [s]he beat us all to it. :-[
– Cbhihe
Mar 5 at 11:05
Thanks everyone for their advices. I didn't know about awk, thanks!
– Multi17
Mar 5 at 17:25
Thanks everyone for their advices. I didn't know about awk, thanks!
– Multi17
Mar 5 at 17:25
add a comment |
In Bash and wc:
IFS=$'n'
while read line; do
wc -w <<< "$line"
done < file.txt
wc counts lines, words, bytes in files. With a shell loop you can make it count words in a line.
Thanks, I was aware about WC -l for lines but not for words, thanks!
– Multi17
Mar 5 at 17:25
add a comment |
In Bash and wc:
IFS=$'n'
while read line; do
wc -w <<< "$line"
done < file.txt
wc counts lines, words, bytes in files. With a shell loop you can make it count words in a line.
Thanks, I was aware about WC -l for lines but not for words, thanks!
– Multi17
Mar 5 at 17:25
add a comment |
In Bash and wc:
IFS=$'n'
while read line; do
wc -w <<< "$line"
done < file.txt
wc counts lines, words, bytes in files. With a shell loop you can make it count words in a line.
In Bash and wc:
IFS=$'n'
while read line; do
wc -w <<< "$line"
done < file.txt
wc counts lines, words, bytes in files. With a shell loop you can make it count words in a line.
edited Mar 4 at 22:08
answered Mar 4 at 19:56
sborskysborsky
811611
811611
Thanks, I was aware about WC -l for lines but not for words, thanks!
– Multi17
Mar 5 at 17:25
add a comment |
Thanks, I was aware about WC -l for lines but not for words, thanks!
– Multi17
Mar 5 at 17:25
Thanks, I was aware about WC -l for lines but not for words, thanks!
– Multi17
Mar 5 at 17:25
Thanks, I was aware about WC -l for lines but not for words, thanks!
– Multi17
Mar 5 at 17:25
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f504326%2fhow-to-count-words-in-a-line%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
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
Required, but never shown
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
Required, but never shown
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
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown

wc would do the necessary job in a loop.
– Puspharaj Selvaraj
Mar 5 at 5:31
Is the output of the
awkscript in the answer what you want to have? If not, what exactly do you want to get?– Bodo
Mar 5 at 11:09