Reputation: 21
I am attempting to create a regex that will find any line that contains exactly one word on it. Words separated by a hyphen or symbol (e.g test-word) or leading white space should still be treated as a single word.
$cat file1
this line has many words
hello
test-hi
this does aswell
Using the regular expression
'/^\s*(\w+)\s$/GM'
Returns only "hello" and ignores "test-hi"
I am able to capture all single words but not ones with hyphens etc!
Upvotes: 1
Views: 555
Reputation: 23677
This is easier to do with awk
, by default it will separate each record into fields based on one or more continuous whitespaces and whitespace at start/end of line won't be part of field calculations
$ awk 'NF==1' ip.txt
hello
test-hi
$ awk 'NF>1' ip.txt
this line has many words
this does aswell
NF
is a built-in variable that indicates number of fields in the input record
Upvotes: 4
Reputation: 522151
Try using \S
to match any non whitespace character:
'/^\s*(\S+)\s$/GM'
Upvotes: 1