0

In my file_*.txt there are two type of words one containing LC1 only and another containing LC1_R.

I would like to grep lines that contain only LC1 not LC1_R. How can I do it?

I tried:

grep "LC1" file_*.txt

then I am getting both lines that contains LC1 as well as those lines which contains LC1_R also.

  • 1
    I bet this's a duplicated question, but I can't find the old one, now. – cuonglm Oct 1 '15 at 4:34
  • Sorry for inconvenience. But, before putting question here I googled it and also search it on Stack-exchange, when I was not able to find it. Then I posted it here. May be my way of searching was not good. I will try to improve it. – ramkrishna Oct 1 '15 at 4:37
  • 2
    This one is an example. – cuonglm Oct 1 '15 at 4:40
1

You can use \b to match the word boundary:

grep '\bLC1\b' file.txt 
1

Awk alternative here. You didn't give much idea about contents of your file,so I'll here's an answer that operates on fields (words) in general

awk '{for(i=1;i<NF;i++) {if ($i=="LC1"){print $0}}}'

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.