Grep and AWK
I have a XML file that I want to pull a number out of.
The file looks like this <WEBSERVER> <actual> <temp> 27.855 </temp> <timestamp> 04.01.2010 </timestamp> </actual> Then it starts over again but with iem 1 , 2 3 etc instead of actual. I want to just pull the number between <temp> and </temp> That term appears a few times in the document as well Would I use grep or awk to do this? |
Quote:
|
Quote:
Code:
while (<>) { |
No each piece of code is on its own line.
The number I need is always on line 4 and is always the only thing on line 4. |
re: grep and awk
I would use both awk and grep, like this:
$ cat YourFile | awk '/<temp>/,/<\/temp>/' | grep -v temp where the awk command prints out all lines between <temp> and <endtemp> pairs, but that includes the <temp> and </temp> lines So: grep -v temp to remove those lines |
Do it all in one call
Code:
awk '/<temp/,/<\/temp/ {if ($0 !~ /temp/) {print } }' temp.txt |
Quote:
Code:
awk 'NR==4' file |
Quote:
2) use grep + awk on BIG files. 3) other than that, just awk will do. |
Quote:
Code:
sed -n '4p' filename Code:
sed -n '/<temp>/,/<\/temp>/{/^[0-9]/p}' filename |
"Tools for the job."
Perl provides a very large library of XML-support routines... all of them thoroughly tested. Use one to read the XML file and then to apply an "XPath expression" to automagically select from it exactly the nodes that you want. Then, output the results as you please. The Unix/Linux environments provide you with "an embarrassment of riches" in terms of "possible ways to do it." What you want to find, then, is the best way. Quite frankly, IMHO, Perl usually is that "best way," hands down. And the reason for this is the astounding "CPAN" library. |
ideally, that should be the case, using libraries to do the job. But for this simple case, there's no need to. Its not that complicated a task.
|
All times are GMT -5. The time now is 04:10 AM. |