Visit Our UNIX and Linux User Community


Taking word count from file and printing in file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Taking word count from file and printing in file
# 1  
Old 08-29-2013
Taking word count from file and printing in file

hi,

i am having a file which contains the below content, i need to take the word count of if and print the file name also

Code:
inputfile.txt
file_name1.txt,type_name1.txt
file_name2.txt,type_name2.txt

i would need the word count of the files like this
Code:
if file_name*.txt 
then
   wc -l file_name*.txt - 1
if type_name*.txt 
then
   wc -l type_name*.txt -2

so my output file format should have
Code:
outputfile.txt
file_name1.txt - 10,type_name1.txt - 20
file_name2.txt - 20,type_name2.txt - 30

considering the word count of the files like below
Code:
file_name1.txt - 11
file_name2.txt - 12
type_name1.txt-22
type_name2.txt - 32


Last edited by vbe; 08-29-2013 at 12:25 PM.. Reason: use code tags adequately not all your text...
# 2  
Old 08-29-2013
You are asking word count but in what you submitted I see line count...
Can you explain what we should understand with your 2 if ?
# 3  
Old 08-29-2013
its acutally line count of the files

my if conditions are it should apply line count for certain files for eg for names containing file_name*.txt(i.e file_name1.txt,file_name2.txt,....so on) in inputfile.txt, i need to apply line count with subtracting 1 and another if condition names containing like
type_name*.txt(i.e type_name1.txt,type_name2.txt,...so on) i need the to apply line count with subtracting 2
# 4  
Old 08-29-2013
Try this bashism, may not work with other shells:
Code:
IFS=","; while read N; do for i in $N; do [[ "$i" =~ type.* ]]; D=$?; echo -n $i - $(($(wc -l < $i) - (2 - $D) )) " "; done; echo; done < file
file_name1.txt - 25  type_name1.txt - 25  
file_name2.txt - 27  type_name2.txt - 28 
when 
wc -l *.txt
  26 file_name1.txt
  28 file_name2.txt
  27 type_name1.txt
  30 type_name2.txt

# 5  
Old 08-29-2013
hi,

the above on is working but what i needed is in generic
for eg:

Code:
file_name1.txt type_name1.txt
. 
.
.
.

by default it should be in condition like 
if file_name*.txt then line count to be subtracted by -1
and if type_name*.txt then line count to be subtracted by -2

and one more thing to be added can i add more rows like in my input file for eg:
file_name1.txt,type_name1.txt,type2_name1.txt
.
.
.

how can i achieve for that

# 6  
Old 08-30-2013
I can't see it shouldn't work for your more generic case.
# 7  
Old 08-30-2013
is there any way out of it if i have n rows like
file_name1.txt,type1_name.txt,anothertype2_name.txt
 

Previous Thread | Next Thread
Test Your Knowledge in Computers #415
Difficulty: Easy
HTML, CSS, and JavaScript are the three core technologies of the World Wide Web.
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

How to append word count at end of file?

Hi guys, I have to append the word count of a file at the end of the file name like this - > "filename_2345" where 2345 is the word count of "filename". How do i achieve this using one command ? I am not allowed to store the word count in a variable and then concatenate. Request your... (1 Reply)
Discussion started by: peter2312
1 Replies

3. UNIX for Dummies Questions & Answers

How do I count how many times a specific word appear in a file (ksh)?

Hi Please can you help how do I count the number of specific characters or words that appear in a file? (8 Replies)
Discussion started by: fretagi
8 Replies

4. Shell Programming and Scripting

Count characters in a csv file and add an word.

Hello, I want to add a sentence to "post column" those who are only less than 30 characters.Thank you very much for your help. "category","title","post" "Z","Zoo","test 54325 test 45363mc." "Z","Zen","rs2w3rsj 2d342dg 2d3s4f23 d23423s23h 2s34s2423g ds232d34 2342." "Z","Zet","test4444... (3 Replies)
Discussion started by: hoo
3 Replies

5. UNIX for Dummies Questions & Answers

how to count number of times each word exist in a file

I'm trying to count the number of times each word in the file exist for example if the file has: today I have a lot to write, but I will not go for it. The main thing is that today I am looking for a way to get each word in this file with a word count after it specifying that this word has... (4 Replies)
Discussion started by: shnkool
4 Replies

6. Shell Programming and Scripting

how to count a word in a file

dear all, i have a requirement to count the errors and display from a file. eg. file1.txt sjdgfjdgfgd ora-0001 sdjgfydh sdukgh7 23 sjdgfjdgfgd ora-0002 sdjgfydhsf34 ew 34v sjdgfjdgfgd ora-0008 sdjgfydh asdf asdfas sjdgfjdgfgd ora-0001 sdjgfydhjkbs ui873 sjdgfjdgfgd ora-0004 sdjgfydh... (9 Replies)
Discussion started by: unx100
9 Replies

7. UNIX for Dummies Questions & Answers

How to count the occurences of a specific word in a file in bash shell

Hello, I want to count the occurences of a specific word in a .txt file in bash shell. Can somebody help me pleaze?? Thanks!!! (2 Replies)
Discussion started by: mskart
2 Replies

8. Shell Programming and Scripting

How to find a count of a word within a file

Hello, I'm looking for a wait to count the number of occurrences of a certain string of characters within a file. The file that I trying to parce has segments within the file that have a header and footer to each segment and I'm trying to do a count of the header string and compare it to a count... (9 Replies)
Discussion started by: bd_joy
9 Replies

9. Shell Programming and Scripting

grep all records in a file and get a word count -perl

Hi, I have a file .. file.txt .. i need to get a total record count in the files into a $variable.. im using perl script thanks (4 Replies)
Discussion started by: meghana
4 Replies

10. Shell Programming and Scripting

Count the number of occurence of perticular word from file

I want to count the number of occurence of perticular word from one text file. Please tell me "less" command is work in ksh or not. If it is not working then instead of that which command will work. :confused: (40 Replies)
Discussion started by: rinku
40 Replies

Featured Tech Videos