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"?

  • can you give me an example? I don't know what i should write. – Thanis Prucksikanont Jan 5 '18 at 15:22
  • 1
    Please add sample input and your desired output for that sample input to your question. – Cyrus Jan 6 '18 at 12:28
  • please check an edited. – Thanis Prucksikanont Jan 6 '18 at 12:33
  • 2
    If you're happy with one or several of the answers, upvote them. If one is solving your issue, accepting it would be the best way of saying "Thank You!" Accepting an answer also indicates to future readers that the answer actually solved the problem. – Kusalananda Mar 4 '18 at 10:27

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

$ grep -o -P 'RT.*?D' <<< "JKIABTHIUCDELKJUCDE"

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.

  • @terdon Seems like the merged question this came from had a critical difference that makes my answer sort of overkill for this question and the other answer here not sufficient for the merged question. (See the added note in my answer.) A little confusing, no? – B Layer Jan 6 '18 at 14:30
  • Well, it's still useful IMO since it shows the point of non-greedy patterns. Maybe it's because I don't find it overkill at all, this is exactly how I'd do it anyway since writing .*? is simpler and shorter than [^D]*. And since the other was under the process of being closed, I felt it was better to save your answer. Especially if other, similar questions are also closed as dupes. – terdon Jan 6 '18 at 14:37
  • Fair enough. I thought perhaps the duplicate was this question but sounds like it was another (I don't see the closed one any more.) – B Layer Jan 6 '18 at 14:40
  • It's this one, it's shown under the "linked " section at the top right. – terdon Jan 6 '18 at 14:43
  • Oh, so this is the dup. I just think they're fundamentally different since one answer here only works for one of them. But, oh well, I'm not gonna lose any sleep over it. – B Layer Jan 6 '18 at 14:46

As the comment said. grep is enough to do the work. Write your pattern as [RT].*D.

To get the first appearance of D, use [RT][^D]*D.

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.

  • my output for D is the last occurance of D. Can I get first occurance of D after R or T?How? – Thanis Prucksikanont Jan 5 '18 at 15:25
  • Yes, see updated answer. – Weijun Zhou Jan 5 '18 at 15:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.