Script to ask for a sentence and then count number of spaces in the sentence


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Script to ask for a sentence and then count number of spaces in the sentence
# 8  
Old 12-21-2009
It seems like there's a slightly simpler method... Since the default FS for awk is whitespace, and the sample sentences appear to only use single-spacing (and hence the number of fields is one greater than the number of spaces), we can simply do:
Code:
echo "How many spaces?" | awk '{print "That sentence has "(NF -1)" spaces"}'

Or not?
# 9  
Old 12-21-2009
Code:
echo "$answer"|awk -F "[ ]" '{print NF-1}'



---------- Post updated at 22:56 ---------- Previous update was at 22:51 ----------

@treesloth. I missed you post as it was on a new page. I came up with the same thing but modified the field separator to use every individual space.

Last edited by Scrutinizer; 12-22-2009 at 03:48 AM.. Reason: Changed "one" to "every individual" for clarity
# 10  
Old 12-21-2009
Odd. Nobody mentioned the wc utility at all.
# 11  
Old 12-21-2009
It was mentioned by the OP to count characters and words. How would you use wc to count blank spaces?
# 12  
Old 12-21-2009
The NR-1 solution only works if there is one space between words. Smilie Mine counts all spaces...
# 13  
Old 12-22-2009
Quote:
Originally Posted by jp2542a
The NR-1 solution only works if there is one space between words. Smilie Mine counts all spaces...
Not if you use "[ ]" as a field separator as I suggested then it does count all the spaces. Just give it a try.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to receive a specific alphanumeric number from a sentence?

Hi, I am quite new to shell scripting. I am facing challenge in retrieving a specific number from a sentence from the log. the number is random and changes everytime in the log. For example, The number of rows updated in table is: 7000 The number of rows updated in table is: 8000 The... (3 Replies)
Discussion started by: arghadeep adity
3 Replies

2. UNIX Desktop Questions & Answers

Normalizing files for sentence count

I have files with many different formats and breaks in odd places. now I want to normalize them to be able to count the sentence in each file 1: I want to count the sentences is they finish with ! . ? 2: but I don't want it to count if there is no space after the Full stop. e.g. S.O.L I have... (6 Replies)
Discussion started by: A-V
6 Replies

3. Shell Programming and Scripting

How to get a number from a grepped sentence of a file?

I want get a number(ID) from a sentence which has been grepped from file using error number. For Example: #!/bin/ksh echo "Enter RRS ID: " read rrs echo "Enter error number:" read err scp -pr ptc-avdbamdw102:/home/icsprd/M3logs/Accurate/logs/corp_post/$rrs.*.err.txt $HOME/daemon_mail/... (7 Replies)
Discussion started by: JayDoshi
7 Replies

4. Shell Programming and Scripting

[grep] how to grep a sentence which has quotation marks "sentence"

I would like to check with grep in this configuration file: { "alt-speed-down": 200, "alt-speed-enabled": true, "alt-speed-time-begin": 1140, "alt-speed-time-day": 127, "...something..." : true, ... } "alt-speed-enabled" (the third line of the file) is setted to... (2 Replies)
Discussion started by: ciro314
2 Replies

5. Shell Programming and Scripting

counting number of sentence

Hi all I want to count total numbers of sentences separated by fullstop (.) in different files under a directory at one go. Any help is appreciated. (3 Replies)
Discussion started by: my_Perl
3 Replies

6. Shell Programming and Scripting

How to split a sentence

Hi, Can anybody help me out, how can I split the sentence, 11111 12-12-2002 1000 23 22222 11-11-2011 2000 24 13131 09-02-2002 like the below format, 11111 12-12-2002 1000 23 22222 11-11-2011 2000 24 etc.... Plz help... Thanks in advance...!! (14 Replies)
Discussion started by: Kattoor
14 Replies

7. Shell Programming and Scripting

Need help in script; [ script to control a sentence & its words ]

Hello friends, I am looking for any sed/awk/python script that can identify the position of a character or word in a file. Well, I prefer sed. <space> is a tab space since I actually dont know how to make the forum editor display a space as such. Sample text ----------- ... (3 Replies)
Discussion started by: frozensmilz
3 Replies

8. Shell Programming and Scripting

Bash script pass sentence in block

Hello, I want to know is it possible to pass a block of sentence using bash. For example, I have a script called Test.sh that takes in $1 and $2. and I'm calling Test.sh in a.sh so in a.sh Test.sh '' 'This is a sentence' Because block are separated by space so when I do that, I get... (6 Replies)
Discussion started by: katrvu
6 Replies

9. UNIX for Dummies Questions & Answers

paste a sentence

Hi, I have a big file like this: BBR 0004186 916 H $$a BBR 0031086 916 H $$a BBR 0032551 916 H $$a BBR 0035481 916 H $$a I would like to add globally a senetence : BBR 0004186 916 H $$a new acquisition How i'll do it? Thanks. (3 Replies)
Discussion started by: gilead29
3 Replies

10. UNIX for Dummies Questions & Answers

grepping for a sentence

Can you grep for a sentence. I have to search logs everyday at work and I was wondering if I could search for a string of words instead of just one. for example, if I had to find this sentence: "Received HTTP message type" How would I grep it (2 Replies)
Discussion started by: eloquent99
2 Replies
Login or Register to Ask a Question