View a file and count all words beginning with specificletter


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers View a file and count all words beginning with specificletter
# 1  
Old 03-30-2019
View a file and count all words beginning with specificletter

I am trying to write a command and need to count all the words within the file which begin with the letter S

I have run this command
Code:
[casupport@docvlapph005 ca]$ grep '^[s]' TheAgileApproach.dat | wc -l
0
[casupport@docvlapph005 ca]$ grep '^[S]' TheAgileApproach.dat | wc -l
1

When I remove the wc -l I see the output as below:
Code:
[casupport@docvlapph005 ca]$ grep '^[s]' TheAgileApproach.dat
[casupport@docvlapph005 ca]$ grep '^[S]' TheAgileApproach.dat
String Filter increment. The most basic implementation that can be used for all data types is shown on the far left. Each version to the right of that adds more functionality.
[casupport@docvlapph005 ca]$

As you can see in the output of point two it brings the whole line but also the word shown is included which should have picked up with the first command?

Is their something specific I am doing wrong?
# 2  
Old 03-30-2019
Code:
'^[s]'

means at the beginning of the line, so the output you got is correct...


Addendum
Oh and wc -l counts the lines If you have 2 occurences on the same line it will be counted as 1...

Last edited by vbe; 03-30-2019 at 12:55 PM.. Reason: addendum
# 3  
Old 03-30-2019
Hey VBE

I want to run so it reads the whole script and prints out the words which start with S or s - what would I edit to do that? I assume I wouldn't use wc -l either?

Last edited by simpsa27; 03-30-2019 at 01:24 PM..
# 4  
Old 03-30-2019
Smilie
If you are trying to learn what can be done by grep ( because others would suggest use sed or awk...)
As I only have my mac laptop at the moment this is what I would do if were to use only grep:
Code:
grep -i -e"^s" -e" s" -o TheAgileApproach.dat|wc -l

If you let me the time to try... I will come back with the result

Im back, result:
Code:
$ cat TheAgileApproach.dat
String Filter increment. The most basic implementation that can be used for all data types is shown on the far left. Each version to the right of that adds more functionality.

$ grep -ie"^s" -e" s" -o TheAgileApproach.dat|wc -l
       2


Addendum
If it works for you , to understand try little bit of the line at a time and see its output...

Last edited by vbe; 03-30-2019 at 01:47 PM.. Reason: addendum and typo...
This User Gave Thanks to vbe For This Post:
# 5  
Old 03-30-2019
Came back with a count of 55 which is correct.

--- Post updated at 04:58 PM ---

What about if I want to print a list of the words that begin with S as when i do it without wc -l it comes up with a list of s

Code:
[casupport@docvlapph005 ca]$ grep -i -e"^s" -e" s" -o TheAgileApproach.dat
 s
 s
 s
 s
 s
 s
 s
 s
 s
 s
 s
 s
 s
 s
 s
 s
 s
 s
 s
 s
 s
 s
 s
 s
 s
 s
 s
 s
 s
 s
 s
 s
 s
 s
S
 s
 s
 s
 s
 s
 s
 s
 s
 s
 s
 s
 s
 s
 s
 s
 s
 s
 s
 s
 s

# 6  
Old 03-30-2019
Hi.

Here are the important parts of a script that seems to do what you wish (including a sample data file). It runs twice, once considering the underscore as a separator, then as a character:
Code:
# Utility functions: print-as-echo, print-line-with-visual-space.
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }

pl " Input data file $FILE:"
head $FILE

pl " Results:"
tr -s '[[:punct:][:space:]]' '\n' < $FILE |
tee t1 |
grep '^[sS]' |
tee t2 |
wc -l

pl  "Content of intermediate files (columnized by local utility):"
for f in t?
do
  pl " File: $f:"
  my-columns $f
done

pl " Results, considering "_" as a character:"
# tr -s '[^\w\s_]' '\n' < $FILE |
grep -o -P '[\w_]+' $FILE |
tee t1 |
grep '^[sS]' |
tee t2 |
wc -l

pl  "Content of intermediate files (columnized by local utility):"
for f in t?
do
  pl " File: $f:"
  my-columns $f
done

producing:
Code:
$ ./s1 data3

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 3.16.0-7-amd64, x86_64
Distribution        : Debian 8.11 (jessie) 
bash GNU bash 4.3.30
tr (GNU coreutils) 8.23
grep (GNU grep) 2.20

-----
 Input data file data3:
(2) SHALL we see?
(3) Is Sheriff Nokill allowing us to Shoot on Sight.
(4) We are agin "Shoot on site" but OK with "shoot on Sight".
(1) Nothing here to See, move along.
(0) Go USA! 
(1) un_Sharpened.
(1) un-Sharpened.
(1) Sharp
(13) total

-----
 Results:
13

-----
Content of intermediate files (columnized by local utility):

-----
 File: t1:
      see     Nokill   Shoot We    on   with  1       See   Go  Sharpened 1    
2     3       allowing on    are   site shoot Nothing move  USA 1         Sharp
SHALL Is      us       Sight agin  but  on    here    along 1   un        13   
we    Sheriff to       4     Shoot OK   Sight to      0     un  Sharpened total

-----
 File: t2:
SHALL Sheriff Sight site  Sight Sharpened Sharp
see   Shoot   Shoot shoot See   Sharpened

-----
 Results, considering _ as a character:
12

-----
Content of intermediate files (columnized by local utility):

-----
 File: t1:
2     Is       to    We    site  on      to    Go           un        total
SHALL Sheriff  Shoot are   but   Sight   See   USA          Sharpened
we    Nokill   on    agin  OK    1       move  1            1        
see   allowing Sight Shoot with  Nothing along un_Sharpened Sharp    
3     us       4     on    shoot here    0     1            13       

-----
 File: t2:
SHALL see Sheriff Shoot Sight Shoot site shoot Sight See Sharpened Sharp

Best wishes ... cheers, drl
This User Gave Thanks to drl For This Post:
# 7  
Old 03-30-2019
How about, given your grep accepts "regular expression extensions" like \b that are not necessarily available in all systems / versions:

Code:
grep -o "\b[sS][^ ]*" <<< "String Filter increment. The most basic implementation that can be used for all data types is shown on the far left. Each version to the right of that adds more functionality."
String
shown

Replace the "here string" with your input file when applying / testing it on your system.
This User Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

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

2. Shell Programming and Scripting

Add words in beginning , end after removing a word in a file

My file has the entries like below... /dev/sds /dev/sdak /dev/sdbc /dev/sdbu I want to make the file like below echo 1 > /sys/block/sds/device/rescan echo 1 > /sys/block/sdak/device/rescan echo 1 > /sys/block/sdbc/device/rescan echo 1 > /sys/block/sdbu/device/rescan (2 Replies)
Discussion started by: saravanapandi
2 Replies

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

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

5. Shell Programming and Scripting

problem to count number of words from file

hi every one i have written this simple shell for counting number of word that user need to find from file but i have get several error when run it. can someone tell me the problem ? echo "Enter the file name" read file echo "enter word" read word for i in \`cat $file` do if then... (1 Reply)
Discussion started by: nimafire
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 Dummies Questions & Answers

Count Number Of lines in text files and append values to beginning of file

Hello, I have 50 text files in a directory called "AllFiles" I want to make a program that will go inside of the "AllFiles" Directory and count the number of lines in each individual text file. Then, the program will calculate how many more lines there are over 400 in each text file and... (7 Replies)
Discussion started by: motoxeryz125
7 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

Help in counting the no of repeated words with count in a file

Hi Pls help in solving my doubt.Iam having file like below file1.txt priya jenny jenny priya raj radhika priya bharti bharti Output required: I need a output like count of repeated words with name for ex: priya 3 jenny 2 (4 Replies)
Discussion started by: bha148
4 Replies

10. UNIX for Dummies Questions & Answers

Remove words beginning with a certain character from a file

Hi, how could you go about removing words that begin with a certain character. assuming that this character is '-' I currently have echo "-hello" | sed s/-/""/ which replaces the leading dash with nothing but I want to remove the whole word, even if there are multiple words beginning... (3 Replies)
Discussion started by: skinnygav
3 Replies
Login or Register to Ask a Question