3

I want to print the pid when finding matched process while the match pattern is inputted:

ps aux | awk -v in="$1" '/in/{print $1}'

It seems the former awk sentence is not right. After checking many results in google like this, I change my script in the following but still cannot work:

ps aux | awk -v in="$1" '/$0 ~ in/{print $1}'

or

ps aux | awk -v in="$1" '($0 ~ in) {print $1}'
1
  • 1
    I think pgrep "$1" would be better option... Commented Feb 9, 2017 at 9:04

1 Answer 1

3

You are fairly close in all your attempts. Problem is that in is a reserved keyword in awk.

You can use:

ps aux | awk -v var="$1" '$0 ~ var {print $1}'

Or else non-regex way:

ps aux | awk -v var="$1" 'index($0, var) {print $1}'
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.