Count words on each line in file using xargs


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Count words on each line in file using xargs
# 1  
Old 03-28-2009
Count words on each line in file using xargs

Hi, im having a problem with xargs, i want to cout word of each line in file, and i HAVE to use xargs, i tried:
cat file | xargs wc -w .....that uses all words in file like name of files and passed then to wc so it worte wc :somewordformfile is not i afile or directory
cat file | xargs -I{} wc -w {} do the same
cat file | xargs -I{} wc -w ...writes number if lines of file 0 ...for each line executes wc -w ...which returns zero,
I know why these dont work, but i dont know how to combine these two commands (yes i need to use wc and xargs only ...cat echo etc ofcourse) to get it. Sorry for my english, any thx for any help
# 2  
Old 03-28-2009
you can do it by awk
Code:
awk '{print NF}' filename

if you want word count along with line
Code:
awk '{print NF" "$0}' filename

if you want to use wc -w only
Code:
while read line ; do
echo "$line"|wc -w
done < filename

# 3  
Old 03-28-2009
yeah that works, i did it using for, but its homework and i have to use xargs to pass each line to wc, but i dont know how, because if i pass "string" to wc's stdin using | ..( echo "string with spaces" | wc -w it returns 3) ....ok but i need the same using xargs for each line
# 4  
Old 03-28-2009
Sorry no homework question here
read the forum rules
closing the thread!!!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace particular words in file based on if finds another words in that line

Hi All, I need one help to replace particular words in file based on if finds another words in that file . i.e. my self is peter@king. i am staying at north sydney. we all are peter@king. How to replace peter to sham if it finds @king in any line of that file. Please help me... (8 Replies)
Discussion started by: Rajib Podder
8 Replies

2. 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

3. Shell Programming and Scripting

Count words from file

hi all how to count words from a text aaa bbb ccc ddd 123 aaa 123 aaa aaa ddd 123 i need to cout hoe many time the words "aaa" and "123" each appears the output should be 4 3 or 4 3 or aaa 4 123 3 thanks (10 Replies)
Discussion started by: sharong
10 Replies

4. UNIX for Dummies Questions & Answers

Count dynamic words in file

Hello, i want built a log analyzer for nginx. Okay and i use it as training for the shell tools. The most what i want i could relize. But i has trouble with dynamic things. I have the IP address extracted and has set the geo localtion for the ip. I would like to count the countries. With... (3 Replies)
Discussion started by: sisihagen
3 Replies

5. Shell Programming and Scripting

How count the number of two words associated with the two words occurring in the file?

Hi , I need to count the number of errors associated with the two words occurring in the file. It's about counting the occurrences of the word "error" for where is the word "index.js". As such the command should look like. Please kindly help. I was trying: grep "error" log.txt | wc -l (1 Reply)
Discussion started by: jmarx
1 Replies

6. Shell Programming and Scripting

count frequency of words in a file

I need to write a shell script "cmn" that, given an integer k, print the k most common words in descending order of frequency. Example Usage: user@ubuntu:/$ cmn 4 < example.txt :b: (3 Replies)
Discussion started by: mohit_iitk
3 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

Count and print all repeating words in a line

Gurus, I have a file containing lines like this : Now, number of words in each line varies. My need is, if a word repeats in a line get it printed. Also total number of repeats. So, the output would be : Any help would be highly appreciated. Thanks & Regards (5 Replies)
Discussion started by: AshwaniSharma09
5 Replies

9. Shell Programming and Scripting

count no of words in a line

hi i have a line "abc,def,ghi,abc,def ,ghi,abc,def,ghi,abc,def ,ghi,abc,def,ghi,abc" I want to print the no of words, words separated by comma please help (3 Replies)
Discussion started by: Satyak
3 Replies

10. Shell Programming and Scripting

count no of words in a line

hi i have a string like str=abc def ghi jkl now i want to count the no of words in the string please help (7 Replies)
Discussion started by: satish@123
7 Replies
Login or Register to Ask a Question