I want to search for string that have [RT]"anything"D which mean first character can either be R or T and the next can be anything until D is appear what should I use is it "egrep"?
If you have GNU grep with it's Perl-compatible Regular Expression (PCRE) support (enabled with
-P) then you can do it this way:
grep -o -P 'RT.*?D' file
We want PCRE because it has the lazy quantifier (
?) which we can use with
.* instead of
.* alone. The latter is greedy and will slurp up everything until the last
$ grep -o 'RT.*D' <<< "JKIRTTHIUCDELKJUCDE" RTTHIUCDELKJUCD $ grep -o -P 'RT.*?D' <<< "JKIABTHIUCDELKJUCDE" RTTHIUCD
Oh, and the
-o flag tells
grep to print only the matching part of a line rather than the default behavior of printing the entire line.
Note: this was merged from a similar, now closed-as-duplicate question that had one important difference: the last part of the pattern was multi-character as opposed to the single
D here. The single-character case can be handled without PCRE (see the other answer here) but the multi-character case requires functionality not available with BRE or ERE syntax.
As the comment said.
grep is enough to do the work. Write your pattern as
To get the first appearance of
There was a caret in the pattern which I have added because of the "first character" in the question. Having realized that the phrase may mean something different I have now removed the carets.