LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 01-04-2010, 04:51 PM   #1
keogk
LQ Newbie
 
Registered: Sep 2005
Posts: 21

Rep: Reputation: 15
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?
 
Old 01-04-2010, 05:04 PM   #2
ozanbaba
Member
 
Registered: May 2003
Location: Tengiz
Distribution: Slackware64 14.1
Posts: 671

Rep: Reputation: 94
Quote:
Originally Posted by keogk View Post
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?
are they on the same line?
 
Old 01-04-2010, 05:11 PM   #3
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Quote:
Originally Posted by keogk View Post
Would I use grep or awk to do this?
awk or sed or even perl as in:
Code:
while (<>) {
  if (/<temp>/../<\/temp>/) {
    next if /<temp>/ || /<\/temp>/;
    print;
  }
}
 
Old 01-04-2010, 05:13 PM   #4
keogk
LQ Newbie
 
Registered: Sep 2005
Posts: 21

Original Poster
Rep: Reputation: 15
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.
 
Old 01-04-2010, 05:15 PM   #5
carolh
LQ Newbie
 
Registered: Mar 2007
Location: Fremont, Ca
Distribution: SuSE, RedHat, FreeBSD, MacOS
Posts: 1

Rep: Reputation: 0
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
 
Old 01-04-2010, 06:02 PM   #6
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 12,429

Rep: Reputation: 1055Reputation: 1055Reputation: 1055Reputation: 1055Reputation: 1055Reputation: 1055Reputation: 1055Reputation: 1055
Do it all in one call
Code:
awk '/<temp/,/<\/temp/ {if ($0 !~ /temp/) {print } }'  temp.txt
Similarly, the perl above can be reduced to a one-liner.
 
Old 01-04-2010, 07:45 PM   #7
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
Quote:
Originally Posted by keogk View Post
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.
Code:
awk 'NR==4' file
 
Old 01-04-2010, 07:47 PM   #8
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
Quote:
Originally Posted by carolh View Post
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
1) no need to use cat.
2) use grep + awk on BIG files.
3) other than that, just awk will do.
 
Old 01-04-2010, 08:44 PM   #9
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
Quote:
Originally Posted by keogk View Post
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.
You are contradicting what you said in the first post.......If the number is always on line 4, then all you need is:
Code:
sed -n '4p' filename
If it is in fact what you first said, then try this:
Code:
sed -n '/<temp>/,/<\/temp>/{/^[0-9]/p}' filename
 
Old 01-04-2010, 08:55 PM   #10
sundialsvcs
Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 5,423

Rep: Reputation: 1158Reputation: 1158Reputation: 1158Reputation: 1158Reputation: 1158Reputation: 1158Reputation: 1158Reputation: 1158Reputation: 1158
"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.
 
Old 01-04-2010, 09:03 PM   #11
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
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.
 
  


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 On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
grep and awk error bazzano Programming 2 04-06-2009 01:01 AM
cat|grep|awk pudhiyavan Programming 6 07-14-2008 02:56 AM
Help with Bash (grep/awk/etc) piercey Programming 12 02-27-2008 11:21 AM
Can I use grep inside awk? Helene Programming 6 04-27-2004 01:58 AM
newbie needs help for grep and awk parker Programming 1 08-12-2003 05:24 AM


All times are GMT -5. The time now is 07:18 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