I'm looking for the string SUCCESS into the file_1 & file_2 which in this case looks for the string and prints it based on whether its there on only file_1 or on both file_1 and file_2 while I'm looking it to print only once either its on both the file of one. How that can be done?

$ grep SUCCESS file_1  file_2


file_1:Host fox_01 is SUCCESS
file_2:Host fox_02 is SUCCESS
file_2:Host fox_01 is SUCCESS

I'm open to any advice or solution, not necessarily grep.

  • So you just want to know if the string SUCCESS is in either file? Your question is a bit hard to understand, it would help if you gave an example of your desired output. – rusty shackleford Apr 24 at 11:29
  • @rustyshackleford, sorry for not making it clear. – krock1516 Apr 24 at 13:16

Try this,

awk '/SUCCESS/ && !a[$0]++' file_1 file_2

Host fox_01 is SUCCESS
Host fox_02 is SUCCESS

will check for the keyword "SUCCESS" and ignores duplicate

  • Excellent @msp9011, I referred the link – krock1516 Apr 24 at 13:13
  • Is there a better way rather using && operator. – krock1516 Apr 24 at 13:26
  • @krock1516 may I know whats the difficulty in using && – msp9011 Apr 24 at 13:30
  • -@msp9011, I see no difficulty but wondering if there can be a better way? Isn't that good question :-) . – krock1516 Apr 24 at 14:08

Use grep -h to not print the file names, and then awk to suppress duplicate lines:

grep -h SUCCESS file_1  file_2 | awk '!seen[$0]++'

or if you wish to sort:

grep -h SUCCESS file_1  file_2 | sort -u

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.