Skip to main content
added 8 characters in body
Source Link
Kusalananda
  • 356.6k
  • 42
  • 739
  • 1.1k

It is not the awk command causing issues, but the pattern you're using with grep.

An unquoted parenthesis is special to the shell. It introduces a sub-shell, but in the context that you're using it, it makes no sense, hence the syntax error.

Therefore, the text Cpu(s) must be quoted as 'Cpu(s)'. If you intend for this to be a piece of literal string you want to search for, you should ideally also use grep with its -F option. This stops grep from interpreting the pattern as a regular expression.

However, using grep together with awk is almost always unnecessary, as awk is perfectly well equipped to do the same thing:

cpu_info () { top -b -n 1 | awk '/Cpu\(s\s)/ { print $2 + $4 }' } 

Here, I escape boththe ( and ) to interpret themit as a literal parenthesesleft parenthesis and not as the start of a grouping of a sub-expression.

It is not the awk command causing issues, but the pattern you're using with grep.

An unquoted parenthesis is special to the shell. It introduces a sub-shell, but in the context that you're using it, it makes no sense, hence the syntax error.

Therefore, the text Cpu(s) must be quoted as 'Cpu(s)'. If you intend for this to be a piece of literal string you want to search for, you should ideally also use grep with its -F option. This stops grep from interpreting the pattern as a regular expression.

However, using grep together with awk is almost always unnecessary, as awk is perfectly well equipped to do the same thing:

cpu_info () { top -b -n 1 | awk '/Cpu\(s\)/ { print $2 + $4 }' } 

Here, I escape both ( and ) to interpret them as literal parentheses and not as a grouping of a sub-expression.

It is not the awk command causing issues, but the pattern you're using with grep.

An unquoted parenthesis is special to the shell. It introduces a sub-shell, but in the context that you're using it, it makes no sense, hence the syntax error.

Therefore, the text Cpu(s) must be quoted as 'Cpu(s)'. If you intend for this to be a piece of literal string you want to search for, you should ideally also use grep with its -F option. This stops grep from interpreting the pattern as a regular expression.

However, using grep together with awk is almost always unnecessary, as awk is perfectly well equipped to do the same thing:

cpu_info () { top -b -n 1 | awk '/Cpu\(s)/ { print $2 + $4 }' } 

Here, I escape the ( to interpret it as a literal left parenthesis and not as the start of a grouping of a sub-expression.

added 2 characters in body
Source Link
Kusalananda
  • 356.6k
  • 42
  • 739
  • 1.1k

It is not the awk command causing issues, but the pattern you're using with grep.

An unquoted parenthesis is special to the shell. It introduces a sub-shell, but in the context that you're using it, it makes no sense, hence the syntax error.

Therefore, the text Cpu(s) must be quoted as 'Cpu(s)'. If you intend for this to be a piece of literal string you want to search for, you should ideally also use grep with its -F option. This stops grep from interpreting the pattern as a regular expression.

AlsoHowever, using grep together with awk is almost always unnessecaryunnecessary, as awk is perfectly well equipped to do the same thing:

cpu_info () { top -b -n1n 1 | awk '/Cpu\(s\)/ { print $2 + $4 }' } 

Here, I escape both ( and ) to interpret them as literal parentheses, and not as a grouping of a sub-expression.

It is not the awk command causing issues, but the pattern you're using with grep.

An unquoted parenthesis is special to the shell. It introduces a sub-shell, but in the context that you're using it, it makes no sense, hence the syntax error.

Therefore, the text Cpu(s) must be quoted as 'Cpu(s)'. If you intend for this to be a piece of literal string you want to search for, you should ideally also use grep with its -F option. This stops grep from interpreting the pattern as a regular expression.

Also, using grep together with awk is almost always unnessecary, as awk is perfectly well equipped to do the same thing:

cpu_info() { top -b -n1 | awk '/Cpu\(s\)/ { print $2 + $4 }' } 

Here I escape both ( and ) to interpret them as literal parentheses, and not as a grouping of a sub-expression.

It is not the awk command causing issues, but the pattern you're using with grep.

An unquoted parenthesis is special to the shell. It introduces a sub-shell, but in the context that you're using it, it makes no sense, hence the syntax error.

Therefore, the text Cpu(s) must be quoted as 'Cpu(s)'. If you intend for this to be a piece of literal string you want to search for, you should ideally also use grep with its -F option. This stops grep from interpreting the pattern as a regular expression.

However, using grep together with awk is almost always unnecessary, as awk is perfectly well equipped to do the same thing:

cpu_info () { top -b -n 1 | awk '/Cpu\(s\)/ { print $2 + $4 }' } 

Here, I escape both ( and ) to interpret them as literal parentheses and not as a grouping of a sub-expression.

Source Link
Kusalananda
  • 356.6k
  • 42
  • 739
  • 1.1k

It is not the awk command causing issues, but the pattern you're using with grep.

An unquoted parenthesis is special to the shell. It introduces a sub-shell, but in the context that you're using it, it makes no sense, hence the syntax error.

Therefore, the text Cpu(s) must be quoted as 'Cpu(s)'. If you intend for this to be a piece of literal string you want to search for, you should ideally also use grep with its -F option. This stops grep from interpreting the pattern as a regular expression.

Also, using grep together with awk is almost always unnessecary, as awk is perfectly well equipped to do the same thing:

cpu_info() { top -b -n1 | awk '/Cpu\(s\)/ { print $2 + $4 }' } 

Here I escape both ( and ) to interpret them as literal parentheses, and not as a grouping of a sub-expression.