LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 03-16-2010, 10:48 AM   #1
puth
LQ Newbie
 
Registered: Feb 2010
Posts: 19

Rep: Reputation: 0
help with shell commands


In a file I have text like
</Printer>
<Printer compu3>
Info compu3
DeviceURI socket://compu3:9100
State Idle
StateTime 1268674467
Accepting Yes
JobSheets none none
QuotaPeriod 0
PageLimit 0
KLimit 0
</Printer>

<Printer compu2>
Info compu2
DeviceURI socket://compu2:9100
State Idle
StateTime 1268674461
Accepting Yes
JobSheets none none
QuotaPeriod 0
PageLimit 0
KLimit 0
</Printer>

For each of the printer compu1,compu2 i need to get the state of the printer from this file.

i am using awk { if($1 ="state") print $2 } to get the state but how to parse only from <Printer compu2> to </Printer> for each printer using shell command?
 
Old 03-16-2010, 11:40 AM   #2
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,498

Rep: Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956
awk by itself processes all the input line by line, so that you can simply print out the name of the printer followed by the state. For example:
Code:
$ awk '/^<Printer /{ sub(/>/,":"); printf "%s ",$2 } /^State /{ print $2 }' file
compu3: Idle
compu2: Idle
If you want to limit the processing to a specific section of the input file, you can try something like
Code:
$ awk '/^<Printer compu2>/,/^<\/Printer>/{ if ( $1 == "State" ) print $2 }' file
Idle
As you can see, in awk two regexp separated by comma are interpreted as a regexp interval, that is every line between the two regexp (included).
 
1 members found this post helpful.
Old 03-16-2010, 01:24 PM   #3
puth
LQ Newbie
 
Registered: Feb 2010
Posts: 19

Original Poster
Rep: Reputation: 0
i am trying to use the second awk above by passing the printername instead of hardcoding to compu2,but i don't get any data returned.
Should it be $0 only or am i missing any escape characters?

echo $printername|awk '/^<Printer $0>/,/^<\/Printer>/{ if ( $1 == "State" ) print $2 }' file

Last edited by puth; 03-16-2010 at 09:04 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
more shell commands mf5160 Linux - Software 3 03-14-2007 01:41 PM
about shell commands nesta Linux - Newbie 3 12-12-2006 05:40 PM
Alias or shell script to confirm 'exit' commands from a shell rose_bud4201 Programming 2 03-08-2006 02:34 PM
commands in shell? kalleanka Linux - General 9 02-01-2006 07:38 AM
shell commands! krishlinux General 1 10-05-2003 04:47 AM


All times are GMT -5. The time now is 08:58 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration