ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Yes, I tried this shell script to extract first the line that contains TOKEN and then use the sed command, but it doesn't work:
Quote:
for this in *.txt;
do
grep TOKEN, sed's/\(.[^"]*\)"\(.[^"]*\)"\(.[^"]*\)"\(.[^"]*\)"\(.[^>]*\)>\(.[^<]*\).*/\6/' $this > "$this.$$"
mv "$this.$$" "$this"
1. first try if the sed command does what you want. Don't write a bash script straight away.
2. The sed command is malformed ("sed's"?!)
3. You don't have to create a temporary file for permanent changes (see sed's -i flag)
4. 'grep TOKEN' is not necessary. Sed can do it for you. Besides there should be the pipe (|) symbol between them, not a comma (,)
No, no ... heed thou the sage advice to use an XML parsing package, and a real programming language to drive it. This is not a suitable application for "a bash script."
For example, look at this link ... http://search.cpan.org/~shlomif/XML-....98/LibXML.pod ... which happens to be a description of a Perl-language binding for the libxml2 library. My purpose here is to point out, if you follow the related links from this page, (a) what a sturdy library like libxml2 can do, and (b) how you do not need to write a C program to use it. If you follow links about "XPath expressions," and if you google for "XSLT stylesheets," you'll begin to get an idea of just what fully-tested options are at your disposal.
Don't "monkey around" trying to "cobble up" a "solution" that will wind up just wasting you a lot of time while producing a decidedly inferior, if not utterly useless bag of code. (And mind you, I am saying this bluntly but in the nicest and most respectful way possible.) There's a big fat deep river in that direction, and no bridge.
Thank you, but I believe there can be an easy way to d it using sed and grep.
Thanks
I'm not at a linux computer now (still at work) so can't test any sed solutions but perhaps you should listen to the majority of the posters in this thread:
Code:
sed/grep are not tools meant to be used for parsing XML documents
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.