number of words in delimited string


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers number of words in delimited string
# 1  
Old 08-23-2010
number of words in delimited string

Hi,

I'm looking for one liner code for counting number of words in a delimited string..

I know about wc -w ..but if i give wc -w a.txt,b.txt it won't work with delimited sting as it was looking for files with a.txt and b.txt

I know there will be a simple solution..i couldn't resolve..myself

Ex:In a string a.txt,b.txt,abacd i need the output as 3..

Please help!
# 2  
Old 08-23-2010
On (of many) ways: Change the delimiter to a newline

Code:
echo "a.txt,b.txt,abacd"|tr ',' '\n'|wc -l
3

# 3  
Old 08-23-2010
Quote:
Originally Posted by ammu
...
Ex:In a string a.txt,b.txt,abacd i need the output as 3..
...
Code:
$
$
$ echo "a.txt,b.txt,abacd" | perl -lne 's/[^,]//g && print length($_)+1'
3
$
$ echo "a.txt,b.txt,abacd" | perl -lne '$_.=","; print s/,//g'
3
$
$ echo "a.txt,b.txt,abacd" | awk '{$0=$0","; print gsub(",",x)}'
3
$
$ # and Perl equivalent of Franklin52's awk script ;)
$ echo "a.txt,b.txt,abacd" | perl -F, -plane '$_=$#F+1'
3
$
$

tyler_durden

Last edited by durden_tyler; 08-23-2010 at 11:28 AM..
# 4  
Old 08-23-2010
Or:
Code:
awk -F, '{print NF}' file

# 5  
Old 08-23-2010
Thank You Guys! It works like a charm!
# 6  
Old 08-24-2010
To get the count of the no of occurrences of the given delimiter (i.e) comma ',' here
Code:
awk -F, '{print NF-1}' file

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace a number in the last line of a delimited file.

Hi all, I am fairly new to UNIX and I was wondering if you could provide me with some help! Lets say i have a file as below : Line 1 Line 2 Line 3 ABC|12|4|2 Now the number 4 in bold, this number will represent the number of row there is in the file excluding the header and footer... (10 Replies)
Discussion started by: Stinza
10 Replies

2. Shell Programming and Scripting

Awkscript to reduce words delimited with comma on right hand to columns

I have a large database with the following structure: Indicword,Indicword,Indicword=English on a line. Not all lines will have this structure. Some might have a single word mapping to a single word in Indic. An example will make this clear ... (4 Replies)
Discussion started by: gimley
4 Replies

3. Shell Programming and Scripting

Help/Advise please for converting space delimited string variable to comma delimited with quote

Hi, I am wanting to create a script that will construct a SQL statement based on a a space delimited string that it read from a config file. Example of the SQL will be For example, it will read a string like "AAA BBB CCC" and assign to a variable named IN_STRING. I then concatenate... (2 Replies)
Discussion started by: newbie_01
2 Replies

4. UNIX for Dummies Questions & Answers

Replace period in a tab delimited file to a number

I have a file like this. It is tab delimited. Unfortunately, the missing data was filled in with a period "." (see the leading lines 1-5 columns) I want to substitute the periods for misisng data with an integer "-999". however, I do not want the global replace to change the other periods seen... (7 Replies)
Discussion started by: genehunter
7 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

Split a free form text delimited by space to words with other fields

Hi, I need your help for below with shell scripting or perl I/P key, Sentence customer1, I am David customer2, I am Taylor O/P Key, Words Customer1,I Customer1,am Customer1,David Customer2,I Customer2,am Customer2,Taylor (4 Replies)
Discussion started by: monishathampi
4 Replies

7. Shell Programming and Scripting

Count number of column in a comma delimited file

I have a comma (,) delimited file. 106232145,"medicare","medicare,medicaid",789 I would like to count the number of fields in each line. I tried the below code awk -F ',' '{print NF-1}' This returns me the result as 5 instead of 4. This is because the awk takes... (9 Replies)
Discussion started by: machomaddy
9 Replies

8. Shell Programming and Scripting

separating comma delimited words

Hi, I have file with text ________________________________ GROUP:firstname1.lastname1,first_name2.last_name2,first_name3.last_name3 HEAD:firstname.lastname ________________________________ I need help to pick the names separately ie.. Need out put as var1 =firstname1.lastname1... (4 Replies)
Discussion started by: rider29
4 Replies

9. Programming

how i display number in words

helo i want to implement the following concept in my project write a c/c++ algorithm for : accept a number from the user not greater than 6 digits and display the number in words i.e. if the input from the user is 18265 then the output should be Eighteen Thousand Two Hundred Sixty Five. if the... (3 Replies)
Discussion started by: amitpansuria
3 Replies

10. Shell Programming and Scripting

To split a string to obtain the words delimited by whitespaces

Please can someone thow some light what is the best way to split a string to obtain the words delimited by whitespaces. (4 Replies)
Discussion started by: Sudhakar333
4 Replies
Login or Register to Ask a Question