How to print from bottom to top?

Hi,

i have a file which contains PID and wanted to execute kill command. but the thing is, when killing PID's needs to kill PID from bottom to top.

Please help

INPUT

21414 sh -c extract.ksh ASA 
21416 /bin/ksh extract.ksh ASA 
21428 /usr/bin/perl -w /var/tmp/tempperl.21416 ASA
21812 sh -c . /opt/aexeo/profiles/user.profile
21822 /bin/sh ASA_EX
21903 /usr/jdk/instances/jdk1.6.0/bin/java -Xms64m -Xmx1024m -Dora

what i wanted to achieve is something like this:

kill -9 21903 21822 21812 21428 21416 21414

will the sort command do the trick

below is the code i used:

for pid in `tail -6 ASA| awk '{$1=$1}{print}' | awk '{print $1}'`
do
   echo "kill -9 $lpid"
done
awk '{a[NR]=$1;next}; END { for (i=NR; i>0; i--) {print a}}' file.test | xargs kill -9

You can also use tac | awk

3 Likes

thanks :b:

---------- Post updated at 11:47 AM ---------- Previous update was at 11:44 AM ----------

one more thing i wanted to have the kill command execute it as

kill -9 21903 21822 21812 21428 21416 21414 21413 16290

in just a single line.

Try :

$ tac file | cut -d' ' -f1 | tr -s '\n' ' ' | xargs kill -9
$ awk 'BEGIN{printf "kill -9 "}{printf $1 OFS}' <(tac file) | sh
$ awk '{printf $1 OFS}' <(tac file) | xargs kill -9
1 Like

You know, xargs already does that for you. Try it alone to test:

awk '{a[NR]=$1;next}; END { for (i=NR; i>0; i--) {print a}}' filename| xargs

Output:

21903 21822 21812 21428 21416 21414

However if you insist on pushing it as such from awk:

awk '{a[NR]=$1;next}; END { list=a[NR]; for (i=NR-1; i>0; i--) {list=list " " a}{print list}}' filename
1 Like