The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Scripting change of text in another file vwgtiturbo Shell Programming and Scripting 7 08-31-2009 10:47 PM
Search text from a file and print text and one previous line too kamranjalal Shell Programming and Scripting 6 01-06-2009 03:27 AM
search and replace a specific text in text file? santosham UNIX for Dummies Questions & Answers 4 06-25-2008 05:53 PM
Building Full-Text Search Applications with Oracle Text iBot Oracle Updates (RSS) 0 04-06-2008 05:10 AM
Perl: Search for string on line then search and replace text Crypto Shell Programming and Scripting 4 01-04-2008 10:24 AM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-07-2009
sharpi03 sharpi03 is offline
Registered User
  
 

Join Date: Sep 2008
Posts: 5
UNIX Text Search Scripting

Hi,

I'm trying to extract XML values for a particular field (MessageId) from a file that has multiple XML data on many lines. For example:

Line1: <?xml version = "1.0"?><xml></xml><xml></xml><xml></xml><xml></xml><xml></xml><field name='MessageId'><value>123456789</value></field><xml></xml><xml></xml><xml></xml><xml></xml>
Line2:<?xml version = "1.0"?><xml></xml><xml></xml><xml></xml><field name='MessageId'><value>987654321</value></field><xml></xml><xml></xml><xml></xml>
.
.
Line n

Please note: MessageId does not appear in the same position from line to line. The red text is the same format for each line.

I haven't been able to find anything on the forums for my situation. Everything I've tried with sed has been returning the whole line as opposed to just the string of numbers for MessageId. My goal is to store them in an array so I can do searches on another data file.

Any help would be greatly appreciated!

Thanks,
Matt
  #2 (permalink)  
Old 10-07-2009
thegeek thegeek is offline
Registered User
  
 

Join Date: Apr 2009
Location: /usr/bin/vim
Posts: 438
are you expecting this ?!

Code:
grep -o "<field name='MessageId'>.*</field>" t1
<field name='MessageId'><value>123456789</value></field>
<field name='MessageId'><value>987654321</value></field>
Kindly put the input in code tags !
  #3 (permalink)  
Old 10-07-2009
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,361

The -o option is not standard:

Code:
grep: invalid option -- o
Usage: grep [OPTION]... PATTERN [FILE]...
Try `grep --help' for more information.
  #4 (permalink)  
Old 10-07-2009
scottn scottn is offline Forum Advisor  
VIP Member
  
 

Join Date: Jun 2009
Location: Zürich, CH
Posts: 1,042
It certainly is not standard.

The man page says "show only the part of the line that matches PATTERN"?

That's what SED is for!

I was at least hoping that the -o option wasn't greedy, but it still grabs what it can.

Is there a UUOO (useless use of options award)?
  #5 (permalink)  
Old 4 Weeks Ago
sharpi03 sharpi03 is offline
Registered User
  
 

Join Date: Sep 2008
Posts: 5
Yep, can't use grep -o.


Any other suggestions?

Thanks!
  #6 (permalink)  
Old 4 Weeks Ago
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,361
Code:
sed 's|.*\(<field.*</field>\).*|\1|'
  #7 (permalink)  
Old 4 Weeks Ago
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,509
both sed /awk suffers from greediness. another approach
Code:
awk 'BEGIN{FS="</field>"}
{
   for (i=1;i<NF;i++){
    gsub(/.*fieldname=/,"",$i)
    print $i
   }
}' file
Reply

Bookmarks

Tags
shell script, xml

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 02:48 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0