LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   help with shell commands (http://www.linuxquestions.org/questions/programming-9/help-with-shell-commands-795804/)

puth 03-16-2010 11:48 AM

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?

colucix 03-16-2010 12:40 PM

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).

puth 03-16-2010 02:24 PM

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


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