Can anyone help me out with this problem?
I'm trying to save the awk output into a variable.
variable = `ps -ef | grep "port 10 -" | grep -v "grep port 10 -"| awk '{printf "%s", $12}'`
printf "$variable"
EDIT: $12 corresponds to a parameter running on that process.
Thanks!
Notice that there's no space after the equal sign.
You can also use
$()
which allows nesting and is readable.The
[p]
is a neat trick to remove the search from showing fromps
@Jeremy If you post the output of
ps -ef | grep "port 10"
, and what you need from the line, it would be more easy to help you getting correct syntaxas noted earlier, setting bash variables does not allow whitespace between the variable name on the LHS, and the variable value on the RHS, of the '=' sign.
awk can do everything and avoid the "awk"ward extra 'grep'. The use of awk's printf is to not add an unnecessary "\n" in the string which would give perl-ish matcher programs conniptions. The variable/parameter expansion for your case in bash doesn't have that issue, so either of these work:
The '-' int the awk record matching pattern removes the need to remove awk itself from the search results.
I think the $() syntax is easier to read...
But the real issue is probably that
$12
should not be qouted with ""Edited since the question was changed, This returns valid data, but it is not clear what the expected output of
ps -ef
is and what is expected in variable.