Im trying to make a bash file that will take another file and count how many lines there are and then count how many words are in each line.
Any help would be great.
Im trying to make a bash file that will take another file and count how many lines there are and then count how many words are in each line.
Any help would be great.
To calculate how many lines you can use,
wc -l filename.txt
To calulcate how many words /line,
awk '{print NR NF}' filename
sed -n '$=' filename
Thanks
Sha
Thanks, but I'm unsure as to how to go about putting this in a script.
Thanks.
Ok i have researched this a little and coming across a few problems.
#!/bin/bash
lc=0;
el=0;
while read
do
el= wc -w;
lc=$(($lc+1));
echo "$el";
echo "$lc";
done < "text.txt"
Its doing a total word count and not a line by line echo with the line number then the amount of words.
#!/bin/bash
lc=0;
el=0;
while read
do el= wc -w;
echo "$el"
done < "text.txt"
while read
do lc=$(($lc+1));
echo "$lc";
done < "text.txt"
and still the wc is not going line by line im thinking it because wc is a built in function is this correct? if so what is an alternative i can use. Also with both word counts wc -w it is skipping the first line regardless of what is in it.
Thanks
el= wc -w;
the above will not work.
because for the wc command, you need the file name or any input
wc <options> filename
or
echo "This is test" | wc -l
and also, you need to use the backtick when you are trying to execute the command and strore the value.
eg:
count=`echo "This is the test" | wc -w`
echo $count
small correction in your script
do el=`wc -w`;
echo $el
Try this, and also check the errors you have made.
#!/bin/bash
lc=0;
el=0;
while read line
do
el=`echo $line|wc -w `;
echo $el
done < "text.txt"
while read
do
lc=$(($lc+1));
done < "text.txt"
echo $lc;
while read
do
el=`wc -w text.txt`
echo "$el";
done < "text.txt"
ok so i have this and it works fine. thanks for that. How would i go about the next step in getting it to word count line by line now that i have the two functions going?
Thanks
See by executing `wc -w text.txt` you are taking all word count in one go, then why are you using loop again.
maintain a variable to hold the line number and increment it each time inside the loop,
and
while read line
do
echo $line|wc -w
done < text.txt
this will print word count, you made the line number to it
#!/bin/bash
el=0;
while read
do
el=`wc -l -w text.txt`
echo "$el";
done < "text.txt"
I have this code working it out puts the toal lines and total word count is there anyway to give it out line by line?
try this
awk '{w=w+NF};END{print "words "w"\nLine "NR}'
#!/bin/bash
el=0;
i=1;
while read line
do
el=`echo $line|wc -w`
echo "LIne No. " $i ",word count" $el;
i=`expr $i + 1`
done < text.txt
while read...
... < file
works bad when file doesn't end with newline char.
This should work:
#!/bin/sh
# Usage: count.sh FILE
wc -l <"$1" # not print the name of FILE
DONE=false
until $DONE; do
read s || DONE=true
words=0
for w in $s; do
words=$((words+1))
done
echo $words
done <"$1"
./count.sh FILE | head -1
for number of lines,
count.sh FILE | tail -n+2
for numbers of words in each line.
It's, of course, easier to use awk.
I have been playing with these scripts abit to understand them but the next thing i want to do is print the lines from the file with 1 word.
would i put this in a loop and say if $el = 1 print line but i cant work out how to do this.
Thanks for all your help.