LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-11-2012, 07:52 AM   #1
kais1
Member
 
Registered: Dec 2008
Posts: 61

Rep: Reputation: 17
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
 
Old 11-11-2012, 08:12 AM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Change the awk program to { gsub( /^.*-/, "", $1 ); print $1 }
 
1 members found this post helpful.
Old 11-11-2012, 08:38 AM   #3
kais1
Member
 
Registered: Dec 2008
Posts: 61

Original Poster
Rep: Reputation: 17
Quote:
Originally Posted by catkin View Post
Change the awk program to { gsub( /^.*-/, "", $1 ); print $1 }
Thanks, but it doesn't help.

Kai
 
Old 11-11-2012, 08:40 AM   #4
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
Blog Entries: 1

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

awk '/027003158/ {..........}'
 
1 members found this post helpful.
Old 11-11-2012, 08:56 AM   #5
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by kais1 View Post
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 }'
 
1 members found this post helpful.
Old 11-11-2012, 10:54 AM   #6
kais1
Member
 
Registered: Dec 2008
Posts: 61

Original Poster
Rep: Reputation: 17
Quote:
Originally Posted by sycamorex View Post
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
 
Old 11-11-2012, 12:06 PM   #7
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
Quote:
Originally Posted by kais1 View Post
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 }'
 
1 members found this post helpful.
Old 11-11-2012, 05:22 PM   #8
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,369

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


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
running series of awk commands from an awk file oreka18 Programming 3 05-16-2012 01:13 AM
awk error awk: line 2: missing } near end of file boscop Linux - Networking 2 04-08-2012 10:49 AM
[SOLVED] call awk from bash script behaves differently to awk from CLI = missing newlines titanium_geek Programming 4 05-26-2011 09:06 PM
[SOLVED] awk: how can I assign value to a shell variable inside awk? quanba Programming 6 03-23-2010 02:18 AM
shell command using awk fields inside awk one71 Programming 6 06-26-2008 04:11 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:39 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration