Count words in a single column


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Count words in a single column
# 1  
Old 03-27-2015
Count words in a single column

Dear All,

I have set of CSV files (comma separated) and each column have some information in them separated by space. Now I want to count them but have not been successful...

Example data
Quote:
1, 23 56, London Oxford Cambridge
4, 45 65 78, London
desired outcome
Quote:
1, 2, 3
1, 3, 1
I have tried few things including the link below.
Code:
for C in $FILES
do
	name=$(basename $C) 
	grep -o ",.*," $C | wc -w >> result/${name}
done

but this gives me count of everything separated by space Smilie

Can you please help me? thanks
# 2  
Old 03-27-2015
Code:
awk -F, '{for(i=1;i<=NF;i++) printf("%d%s", split($i,a, " "), (i==NF)?ORS:OFS)}' OFS=',' myFile

This User Gave Thanks to vgersh99 For This Post:
# 3  
Old 03-27-2015
magic ... thank you so much
tired awk but not with split and OFS

now it words perfectly
# 4  
Old 03-27-2015
Bit simplified version of vgersh99's solution

Code:
$ awk -F, '{for(i=1; i<=NF; i++)$i=split($i,a," ")}1' OFS="," file
1,2,3
1,3,1

This User Gave Thanks to Akshay Hegde For This Post:
# 5  
Old 03-30-2015
Thanks a lot from both of you ...
But both add random 0 at the end of few files ...
the ones I have resided are the ones that do not have values for the last column
e.g. example data
Quote:
1, 23 56, London Oxford Cambridge
4, 45 65 78,
instead of
Quote:
1, 2, 3
1, 3, 0
it is publishing
Quote:
1, 2, 3
1, 3, 0, 0
Can you please help me?
# 6  
Old 03-30-2015
Both proposals deliver the desired output, not the errorneous one you posted. Are you sure there's no hidden control chars in the input file? Did yo run the codes exactly as posted?
This User Gave Thanks to RudiC For This Post:
# 7  
Old 03-30-2015
With that input, we're seeing the output you seem to want.
Please post the output from the command:
Code:
od -bc file

where file is the input file you fed to either of the awk scripts provided by vgersh99 and Akshay Hegde.
This User Gave Thanks to Don Cragun For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Count unique words

Dear all, I would like to know how to list and count unique words in thousands number of text files. Please help me out thanks in advance (9 Replies)
Discussion started by: imranrasheedamu
9 Replies

2. Shell Programming and Scripting

Bring values in the second column into single line (comma sep) for uniq value in the first column

I want to bring values in the second column into single line for uniq value in the first column. My input jvm01, Web 2.0 Feature Pack Library jvm01, IBM WebSphere JAX-RS jvm01, Custom01 Shared Library jvm02, Web 2.0 Feature Pack Library jvm02, IBM WebSphere JAX-RS jvm03, Web 2.0 Feature... (10 Replies)
Discussion started by: kchinnam
10 Replies

3. Shell Programming and Scripting

Paste 2 single column files to a single file

Hi, I have 2 csv/txt files with single columns. I am trying to merge them using paste, but its not working.. output3.csv: flowerbomb everlon-jewelry sofft steve-madden dolce-gabbana-watchoutput2.csv: http://www1.abc.com/cms/slp/2/Flowerbomb http://www1.abc.com/cms/slp/2/Everlon-Jewelry... (5 Replies)
Discussion started by: ajayakunuri
5 Replies

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

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

Converting Single Column into Multiple rows, but with strings to specific tab column

Dear fellows, I need your help. I'm trying to write a script to convert a single column into multiple rows. But it need to recognize the beginning of the string and set it to its specific Column number. Each Line (loop) begins with digit (RANGE). At this moment it's kind of working, but it... (6 Replies)
Discussion started by: AK47
6 Replies

7. Shell Programming and Scripting

Multiple lines in a single column to be merged as a single line for a record

Hi, I have a requirement with, No~Dt~Notes 1~2011/08/1~"aaa bbb ccc ddd eee fff ggg hhh" Single column alone got splitted into multiple lines. I require the output as No~Dt~Notes 1~2011/08/1~"aaa<>bbb<>ccc<>ddd<>eee<>fff<>ggg<>hhh" mean to say those new lines to be... (1 Reply)
Discussion started by: Bhuvaneswari
1 Replies

8. Shell Programming and Scripting

Shell script to find out words, replace them and count words

hello, i 'd like your help about a bash script which: 1. finds inside the html file (it is attached with my post) the code number of the Latest Stable Kernel, 2.finds the link which leads to the download location of the Latest Stable Kernel version, (the right link should lead to the file... (3 Replies)
Discussion started by: alex83
3 Replies

9. Shell Programming and Scripting

Count words

Hi, does anyone know the command to count words by its length. I need to Count the number of five letter words that I have in a file with thousand of words. thanks (3 Replies)
Discussion started by: fabioamaury
3 Replies

10. 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
Login or Register to Ask a Question