UNIX Text Search Scripting


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting UNIX Text Search Scripting
# 1  
Old 10-07-2009
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  
Old 10-07-2009
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  
Old 10-07-2009

The -o option is not standard:

Code:
grep: invalid option -- o
Usage: grep [OPTION]... PATTERN [FILE]...
Try `grep --help' for more information.

# 4  
Old 10-07-2009
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  
Old 10-23-2009
Yep, can't use grep -o.


Any other suggestions?

Thanks!
# 6  
Old 10-23-2009
Code:
sed 's|.*\(<field.*</field>\).*|\1|'

# 7  
Old 10-23-2009
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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to search a text in file and retrieve required lines following it with UNIX command?

I have requirement to search for a text in the file and retrieve required lines that is user defined with unix command. Eg: Find the text UNIX in the below file and need to return Test 8 & Test 9 Test 1 Test 2 Test 3 Test 4 UNIX Test 5 Test 6 Test 7 Test 8 Test 9 Result can... (8 Replies)
Discussion started by: Arunkumarsak4
8 Replies

2. Shell Programming and Scripting

Read csv file, convert the data and make one text file in UNIX shell scripting

I have input data looks like this which is a part of a csv file 7,1265,76548,"0102:04" 8,1266,76545,"0112:04" I need to make the output data should look like this and the output data will be part of text file: 7|1265000 |7654899 |A| 8|12660000 |76545999 |B| The logic behind the... (6 Replies)
Discussion started by: RJG
6 Replies

3. Shell Programming and Scripting

UNIX Scripting help to input string and search a file to find

Hi Don, this is not homework question. I work for a Credit card company and my development goal this year is to learn Unix. I would love if others can help me get started, thanks. Hi everyone I am new to Unix and need help writing a script that can ask user for an input, then search that input... (2 Replies)
Discussion started by: 12ic11
2 Replies

4. Shell Programming and Scripting

UNIX Scripting help to input string and search a file to find

Hi everyone, I am new to Unix and need help writing a script that can ask user for an input, then search that input within a file I know will have to use the read and grep commands, anyone can give me somewhere to start would help Task: Write a script to display which volume pool a given... (1 Reply)
Discussion started by: 12ic11
1 Replies

5. UNIX for Dummies Questions & Answers

UNIX Scripting help to input string and search a file to find

Hi everyone, I am new to Unix and need help writing a script that can ask user for an input, then search that input within a file I know will have to use the read and grep commands, anyone can give me somewhere to start would help Task: Write a script to display... (1 Reply)
Discussion started by: 12ic11
1 Replies

6. Shell Programming and Scripting

Read in search strings from text file, search for string in second text file and output to CSV

Hi guys, I have a text file named file1.txt that is formatted like this: 001 , ID , 20000 002 , Name , Brandon 003 , Phone_Number , 616-234-1999 004 , SSNumber , 234-23-234 005 , Model , Toyota 007 , Engine ,V8 008 , GPS , OFF and I have file2.txt formatted like this: ... (2 Replies)
Discussion started by: An0mander
2 Replies

7. Shell Programming and Scripting

How to search and append words in the same file using unix scripting file operations

Hi , I have a file myhost.txt which contains below, 127.0.0.1 localhost 1.17.1.5 atrpx958 11.17.10.11 atrpx958zone nsybhost I need to append words only after "atrpx958" like 'myhost' and 'libhost' and not after atrpx958zone. How to search the word atrpx958(which is hostname) only,... (5 Replies)
Discussion started by: gsreeni
5 Replies

8. Shell Programming and Scripting

generate tabular output from an input text file in unix shell scripting

Hi, I have the output (as below) which i want it to be in a table. For e.g. space utilization in PSE on path /logs is 0% space utilization in PSE on path /logs/tuxedo/tuxlsp is 16% space utilization in PSE on path /ldvarlsp/lsp/log is 37% space utilization in PSE on path /home is 6%... (7 Replies)
Discussion started by: pkbond
7 Replies

9. UNIX for Dummies Questions & Answers

Unix search a string in the text file

File name : Sample.txt Actually i would like to read <schema>Oracle<schema> string from input file and return only once database as my output. Please advise me. Moved to appropriate forum. (1 Reply)
Discussion started by: balajikalai
1 Replies

10. Shell Programming and Scripting

Perl: Search for string on line then search and replace text

Hi All, I have a file that I need to be able to find a pattern match on a line, search that line for a text pattern, and replace that text. An example of 4 lines in my file is: 1. MatchText_randomNumberOfText moreData ReplaceMe moreData 2. MatchText_randomNumberOfText moreData moreData... (4 Replies)
Discussion started by: Crypto
4 Replies
Login or Register to Ask a Question