-1

I have the output of a ps -ef command that returns hundreds of lines, and I need to grep for a pattern that is mostly identical, except for the number at the end, and have a single instance of each pattern returned. Example:

this particular ps -ef command outputs dozens of lines, each containing the string svc-0 or svc-5 or any number in between 0 and 5, the svc numbers are randomly assigned. If I do grep -Eo 'svc-[0-5]' it will just pattern-match and return each instance from every line. I simply need to confirm the presence of svc-0 and svc-5. The output I'm hoping for is:

svc-0
svc-5

I can't just add an -m2 to the grep, because the svc lines are often repeated multiple times in a row, like:

svc-5
svc-5
svc-5
svc-5
svc-5
svc-0
svc-0
svc-0
svc-0
svc-0
  • 1
    can you | sort -u ? – thrig Oct 16 '18 at 14:52
0

You could approach it with an awk filter that looks for the two desired patterns; if both are found, then exit successfully, otherwise exit with "failure":

ps -ef ... | awk '/svc-0/ { zero++; } /svc-5/ { five++; } END { if (zero && five) exit 0; exit 1; }'

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.