Parse out part of line in text document


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Parse out part of line in text document
# 1  
Old 10-21-2010
Parse out part of line in text document

I have an XML file that I am attempting to create a new text document from it with only one of the pieces of data printed. Here's an example:

Source:
Code:
<?xml version="1.0"?>
<Reply Success="TRUE">
    <ProfileList NrOfProfiles.DWD="645">
        <Profile_0 Name="Test" Description="A description." Category="System" ABCD="{FF350E61-4FFF-4600-BFFF-3B27DD4BA746}"/>
        <Profile_1 Name="Test" Description="Another description" Category="System" ProfileFlags.DWD="5" ABCD="{73F11780-FFFF-420A-B0FC-FFFFF62B2AA0}"/>
        <Profile_2 Name="Test" Description="Another description." Category="System" ProfileFlags.DWD="5" ABCD="{4247D9B0-5E89-FFFF-B2C3-FFFF4771BC4C}"/>
        <Profile_3 Name="Test" Description="Another description" Category="System" ProfileFlags.DWD="5" ABCD="{EAA268CD-42F1-4B49-B7A9-608FFFF56748}"/>
        <Profile_4 Name="Test" Description="Another description" Category="System" ProfileFlags.DWD="5" ABCD="{26BFFFF1-FFFF-4D3C-FFFF-608D23162123}"/>
    </ProfileList>
</Reply>

Desired Output:
Code:
{FF350E61-4FFF-4600-BFFF-3B27DD4BA746}
{73F11780-FFFF-420A-B0FC-FFFFF62B2AA0}
{4247D9B0-5E89-FFFF-B2C3-FFFF4771BC4C}
{EAA268CD-42F1-4B49-B7A9-608FFFF56748}
{26BFFFF1-FFFF-4D3C-FFFF-608D23162123}

I was thinking of using a combination of grep and sed, but I have no idea the proper sytax of the sed command. Any thoughts?
# 2  
Old 10-21-2010
try this:
Code:
sed -n '/<Profile_/s/.*ABCD="\(.*\)".*/\1/p' file

or maybe :

Code:
awk  '/-/{printf("{%s}\n",$1)}' RS="[{}]" file


Last edited by saw7; 10-21-2010 at 10:47 PM..
This User Gave Thanks to saw7 For This Post:
# 3  
Old 10-22-2010
Thanks so much saw7! The sed command is exactly what I'm looking for. Now the next step will be for me to be able to write that type code on my own...
# 4  
Old 10-22-2010
Code:
#!/usr/bin/env ruby
require 'rexml/document'
include REXML
File.open(file) do |doc|
  xml = Document.new(doc)
  xml.elements.each("Reply/ProfileList") do |elem|
    elem.elements.each {|x| puts x.attributes["ABCD"]}
  end
end

Code:
$ ruby parser.rb xmlfile
{FF350E61-4FFF-4600-BFFF-3B27DD4BA746}
{73F11780-FFFF-420A-B0FC-FFFFF62B2AA0}
{4247D9B0-5E89-FFFF-B2C3-FFFF4771BC4C}
{EAA268CD-42F1-4B49-B7A9-608FFFF56748}
{26BFFFF1-FFFF-4D3C-FFFF-608D23162123}

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Script to delete files line by line in .txt document

Friends, I am in need to delete files from a directory on a regular basis. I want to place all the files to be deleted in delete .txt file and require a script to delete files, line by line from this delete.txt file. Please help me with this script as am new to unix scripting. (3 Replies)
Discussion started by: fop4658
3 Replies

2. Shell Programming and Scripting

Reading text file, comparing a value in a line, and placing only part of the line in a variable?

I need some help. I would like to read in a text file. Take a variable such as ROW-D-01, compare it to what's in one line in the text file such as PROD/VM/ROW-D-01 and only input PROD/VM into a variable without the /ROW-D-01. Is this possible? any help is appreciated. (2 Replies)
Discussion started by: xChristopher
2 Replies

3. Shell Programming and Scripting

[Solved] Printing a part of the last line of the specific part of a file

Hi, I have 80 large files, from which I want to get a specific value to run a Bash script. Firstly, I want to get the part of a file which contains this: Name =A xxxxxx yyyyyy zzzzzz aaaaaa bbbbbb Value = 57 This is necessary because in a file there are written more lines which... (6 Replies)
Discussion started by: wenclu
6 Replies

4. Shell Programming and Scripting

replace part of text of a line

Gurus, You know, I believe you do:-), the comnand uname -r give you the kernel version: serverA:~# uname -r 2.6.26-1-xen-amd64So, I want to replace this output inside in the line below that is inside the file: kernel = '/boot/vmlinuz-2.6.26-1-xen-amd64'Suppose, you move this file to ther... (2 Replies)
Discussion started by: iga3725
2 Replies

5. Shell Programming and Scripting

Finding a string in a text file and posting part of the line

What would be the most succinct way of doing this (preferably in 1 line, maybe 2): searching the first 10 characters of every line in a text file for a specific string, and if it was found, print out characters 11-20 of the line on which the string was found. In this case, it's known that there... (13 Replies)
Discussion started by: busdude
13 Replies

6. Shell Programming and Scripting

Find a special string in a text document

Hi, i got stuck in creating a search job. I have a text file which looks like this: ====================== Backup - OK - Backup - OK - Backup - ERROR - Backup - WARNING - ====================== I want to search the text file for the string ERROR. If this string is present in the... (10 Replies)
Discussion started by: lnino
10 Replies

7. UNIX for Advanced & Expert Users

how do you parse 1 line at a time of file1 ie. line(n) each line into new file

File 1 <html>ta da....unique file name I want to give file=>343...</html> <html>da ta 234 </html> <html>pa da 542 </html> and so on... File 2 343 234 542 and so on, each line in File 1 one also corresponds with each line in File 2 I have tried several grep, sed, while .. read, do,... (4 Replies)
Discussion started by: web_developer
4 Replies

8. UNIX for Dummies Questions & Answers

Comparing file names to text document

Hi All, I'm really new to Unix scripts and commands but i think i'm eventually getting the hang of some of it. I have a task which is to create some kind of script which compares the file names in a directory, with the associated file name in a .txt file. We send out some data and Unix has a... (1 Reply)
Discussion started by: gman
1 Replies

9. UNIX for Advanced & Expert Users

Mutt - Word Document or Formatted text as a Message

Hi, I am writing a mailing script by using mutt command. I that i have facing a issues. because, i want to send Some Formatted text as the mail message. but, i try to send the Word Document file as the Mail message. it shows some junk characters in the mail. :confused:I think the mutt command is... (1 Reply)
Discussion started by: krsenkumar
1 Replies

10. Shell Programming and Scripting

Appending to the first line of a document

Hi all, I'm new to the forums and a total beginner with Unix. I've been given the seemingly simple task of writing a script that asks for two arguments: the first argument being a line of text, the second argument being a document. The script has to take the first argument of text and append it to... (5 Replies)
Discussion started by: oldhoi
5 Replies
Login or Register to Ask a Question