Tuesday, July 26, 2022

[SOLVED] Use grep to search for words beginning with letter "s"

Issue

I'm trying to use grep to look through a file, and find words starting with the lowercase letter "s". Snippet of the file:

sjpope   pts/2    161.133.12.95    10:21am 43.00s  0.13s  0.01s  man bc
rmschalk pts/3    161.133.9.147    10:22am  1.00s  0.10s  0.02s  vi testb
jntrudel pts/4    161.133.9.11     10:23am  2.00s  0.09s  0.00s  vi testb
tjbanks  pts/5    161.133.9.70     10:41am  8.00s  0.06s  0.04s  -ksh

I want the output to have line stating with "s".


Solution

For all these that want to search for words starting with given letter not for lines this one-liner will do:

grep -E '\bs' file.txt # all words starting with s
grep -E 's\b' file.txt # all words ending with s

I know that this is non standard grep behavior (\b regex anchor that means word break is not in extended regular expressions standard) but it works on most modern systems.



Answered By - csharpfolk
Answer Checked By - Dawn Plyler (WPSolving Volunteer)