Visit Our UNIX and Linux User Community


Line and word count.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Line and word count.
# 8  
Old 06-06-2011
Code:
small correction in your script 
do el=`wc -w`;
echo $el

# 9  
Old 06-06-2011
Quote:
Originally Posted by syco__
Ok i have researched this a little and coming across a few problems.

2. so i tried this.

Code:
#!/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

Try this, and also check the errors you have made.
Code:
#!/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;

# 10  
Old 06-06-2011
Code:
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
# 11  
Old 06-06-2011
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

Code:
while read line
do
  echo $line|wc -w 
done < text.txt

this will print word count, you made the line number to it

Last edited by Franklin52; 06-08-2011 at 05:00 AM.. Reason: code tags
This User Gave Thanks to kumaran_5555 For This Post:
# 12  
Old 06-06-2011
Code:
#!/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?
# 13  
Old 06-06-2011
try this
Code:
 
awk '{w=w+NF};END{print "words "w"\nLine "NR}'

# 14  
Old 06-06-2011
Code:
#!/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


Previous Thread | Next Thread
Test Your Knowledge in Computers #882
Difficulty: Easy
In October 2013 National Geographic released a file titled American Blackout which dealt with an imagined large-scale cyber attack on SCADA and the US electrical grid.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

UNIX script to check word count of each word in file

I am trying to figure out to find word count of each word from my file sample file hi how are you hi are you ok sample out put hi 1 how 1 are 1 you 1 hi 1 are 1 you 1 ok 1 wc -l filename is not helping , i think we will have to split the lines and count and then print and also... (4 Replies)
Discussion started by: mirwasim
4 Replies

2. Shell Programming and Scripting

Count the pipes "|" in line and delete line if count greter then number.

Hello, I have been working on Awk/sed one liner which counts the number of occurrences of '|' in pipe separated lines of file and delete the line from files if count exceeds "17". i.e need to get records having exact 17 pipe separated fields(no more or less) currently i have below : awk... (1 Reply)
Discussion started by: ketanraut
1 Replies

3. UNIX for Advanced & Expert Users

Sort words based on word count on each line

Hi Folks :) I have a .txt file with thousands of words. I'm trying to sort the lines in order based on number of words per line. Example from: word word word word word word word word word word word word word word word word to desired output: word (2 Replies)
Discussion started by: martinsmith
2 Replies

4. Shell Programming and Scripting

Find word in a line and output in which line the word occurs / no. of times it occurred

I have a file: file.txt, which contains the following data in it. This is a file, my name is Karl, what is this process, karl is karl junior, file is a test file, file's name is file.txt My name is not Karl, my name is Karl Joey What is your name? Do you know your name and... (3 Replies)
Discussion started by: anuragpgtgerman
3 Replies

5. Shell Programming and Scripting

Read a File line by line and split into array word by word

Hi All, Hope you guys had a wonderful weekend I have a scenario where in which I have to read a file line by line and check for few words before redirecting to a file I have searched the forum but,either those answers dint work (perhaps because of my wrong under standing of how IFS... (6 Replies)
Discussion started by: Kingcobra
6 Replies

6. UNIX for Advanced & Expert Users

Count specific word or character per line

Hi, I need help regarding counting specific word or character per line and validate it against a specific number i.e 10. And if number of character equals the specific number then that line will be part of the output. Specific number = 6 Specific word or char = || Sample data:... (1 Reply)
Discussion started by: janzper
1 Replies

7. UNIX for Advanced & Expert Users

cut words based on the word count of a line

I would like to cut words based on the word count of a line. This over here inspired me with some ideas but I wasn't able to get what I needed. https://www.unix.com/shell-programming-scripting/105841-count-words-each-line-file-using-xargs.html If the line has 6 words I would like to use this.... (8 Replies)
Discussion started by: cokedude
8 Replies

8. Shell Programming and Scripting

Print word 1 in line 1 and word 2 in line 2 if it matches a pattern

i have a file in this pattern MATCH1 word1 IMAGE word3 word4 MATCH2 word1 word2 word3 word4 MATCH2 word1 word2 word3 word4 MATCH2 word1 word2 word3 word4 MATCH2 word1 word2 word3 word4 MATCH1 word1 IMAGE word3 word4 MATCH2 word1 word2 word3 word4 MATCH2 word1 word2 word3 word4 MATCH2 word1... (7 Replies)
Discussion started by: bangaram
7 Replies

9. Shell Programming and Scripting

Word count of lines ending with certain word

Hi all, I am trying to write a command that can help me count the number of lines in the /etc/passwd file ending in bash. I have read through other threads but am yet to find one indicating how to locate a specifc word at the end of a line. I know i will need to use the wc command but when i... (8 Replies)
Discussion started by: warlock129
8 Replies

10. Shell Programming and Scripting

Looking for a single line to count how many times one character occurs in a word...

I've been looking on the internet, and haven't found anything simple enough to use in my code. All I want to do is count how many times "-" occurs in a string of characters (as a package name). It seems it should be very simple, and shouldn't require more than one line to accomplish. And this is... (2 Replies)
Discussion started by: Shingoshi
2 Replies

Featured Tech Videos