LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   AWK help (https://www.linuxquestions.org/questions/linux-newbie-8/awk-help-4175436645/)

kais1 11-11-2012 07:52 AM

AWK help
 
hai all,

Very frequrently we need to kill all pending jobs for some printers and the queue has so many process..So am preparing the below script to kill the process

lpstat gives the below

hp2_027003158-1048902 oracle 23552 Thu 26 Apr 2012 09:27:48 AM AST
hp2_027003158-1049585 oracle 22528 Sat 28 Apr 2012 07:37:13 AM AST
hp2_027003158-1049587 oracle 21504 Sat 28 Apr 2012 07:37:59 AM AST
hp2_027003158-1051259 oracle 27648 Sun 29 Apr 2012 01:46:40 PM AST
hp2_027003158-1051312 oracle 25600 Sun 29 Apr 2012 02:03:17 PM AST


{code}
p=`lpstat | grep -i 027003158 grep -v grep | awk '{ print $1}'`
for i in $p
do
echo $i
kill -9 $i
done
{code}

But $1 in awk retrieves the complete like below hp2_027003158-1048902 .How can I take the ID only 1048902 out of it ?

Kai

catkin 11-11-2012 08:12 AM

Change the awk program to { gsub( /^.*-/, "", $1 ); print $1 }

kais1 11-11-2012 08:38 AM

Quote:

Originally Posted by catkin (Post 4826977)
Change the awk program to { gsub( /^.*-/, "", $1 ); print $1 }

Thanks, but it doesn't help.

Kai

sycamorex 11-11-2012 08:40 AM

Also, I don't think you need grep there at all. Awk can do it as well:

awk '/027003158/ {..........}'

catkin 11-11-2012 08:56 AM

Quote:

Originally Posted by kais1 (Post 4826993)
Thanks, but it doesn't help.

Sorry -- I forgot it is in a bash command, not an awk script. So it needs quoting
Code:

'{ gsub( /^.*-/, "", $1 ); print $1 }'

kais1 11-11-2012 10:54 AM

Quote:

Originally Posted by sycamorex (Post 4826994)
Also, I don't think you need grep there at all. Awk can do it as well:

awk '/027003158/ {..........}'

Thanks. Am new to scripting and am exploring it now.

{code}
p=`lpstat | grep -i 027003158 grep -v grep | awk '{ print $1}'`
{code}

Can you help me in inserting awk '/027003158/ {..........} in my above command ..

Thanks
Kai

sycamorex 11-11-2012 12:06 PM

Quote:

Originally Posted by kais1 (Post 4827062)
Thanks. Am new to scripting and am exploring it now.

{code}
p=`lpstat | grep -i 027003158 grep -v grep | awk '{ print $1}'`
{code}

Can you help me in inserting awk '/027003158/ {..........} in my above command ..

Thanks
Kai

Using the solution that catkin provided, this should work:

Code:

lpstat | awk '/027003158/ { gsub( /^.*-/, "", $1 ); print $1 }'

chrism01 11-11-2012 05:22 PM

If these are print jobs, shouldn't you use cancel http://linux.die.net/man/1/cancel-cups ?


All times are GMT -5. The time now is 01:30 PM.