Reputation: 23
I tried something like this
sed 's/^[ \t]*//' file.txt | grep "^[A-Z].* "
but it will show only the lines that start with words starting with an uppercase.
file.txt content:
Something1 something2
word1 Word2
this is lower
The output will be Something1 something2 but I will like for it to also show the second line because also has a word that starts with an uppercase letter.
Upvotes: 2
Views: 1389
Reputation: 141493
How do you display all the words
That's simple:
grep -wo '[A-Z]\w*'
Upvotes: 0
Reputation: 627083
With GNU grep
, you can use
grep '\<[[:upper:]]' file
grep '\b[[:upper:]]' file
NOTE:
\<
- a leading word boundary (\b
is a word boundary)[[:upper:]]
- any uppercase letter.See the online demo:
#!/bin/bash
s='Something1 something2
word1 Word2
this is lower
папа Петя'
grep '\<[[:upper:]]' <<< "$s"
Output:
Something1 something2
word1 Word2
папа Петя
Upvotes: 1
Reputation: 26690
With GNU grep grep -P "[A-Z]+\w*" file.txt
will work. Or, as @Shawn said in the comment below, grep -P '\b[A-Z]' file.txt
will also work. If you only want the words, and not the entire line, grep -Po "[A-Z]+\w*" file.txt
will give you the individual words.
Upvotes: 1