LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-16-2017, 12:03 PM   #1
spatil20
LQ Newbie
 
Registered: May 2015
Posts: 21

Rep: Reputation: Disabled
XML value need to find and replace


i have XML file ,in the file some of the line start with <SQL*> and </SQL>,so i need find X column in between <SQL*> and </SQL> and need to replace with trim(x) in the XML file.

I/0
<SQL><SelectStatement modified=\\'1\\' type=\\'string\\'><![CDATA[
SEL A.COLUMN_NMAE1,
A.FIND_REPLCAE
FROM TABLE_NAME
\><Tables collapsed=\\'1\\'></Tables><Parameters collapsed=\\'1\\'></Parameters><Columns collapsed=\\'1\\'></Columns></SelectStatement><SQL>

<SQL><SelectStatement modified='1' type='string'><![CDATA[ SEL A.COLUMN_NMAE1, \(D)\(A) A.FIND_REPLCAE FROM TABLE_NAME]]><Tables collapsed='1'></Tables><Parameters collapsed='1'></Parameters><Columns collapsed='1'></Columns></SelectStatement></SQL>

O/P

<SQL><SelectStatement modified=\\'1\\' type=\\'string\\'><![CDATA[
SEL A.COLUMN_NMAE1,
Trim(A.FIND_REPLCAE)
FROM TABLE_NAME
\><Tables collapsed=\\'1\\'></Tables><Parameters collapsed=\\'1\\'></Parameters><Columns collapsed=\\'1\\'></Columns></SelectStatement><SQL>

<SQL><SelectStatement modified='1' type='string'><![CDATA[ SEL A.COLUMN_NMAE1, \(D)\(A) Trim(A.FIND_REPLCAE) FROM TABLE_NAME]]><Tables collapsed='1'></Tables><Parameters collapsed='1'></Parameters><Columns collapsed='1'></Columns></SelectStatement></SQL>



#!/bin/bash

for a in `sed -n '/<SQL>/,/<\/SQL>/H; /<SQL>/h; /\/SQL/{x;s/<SQL>\(.*[^\n]\)\n*<\/SQL>/\1/p;}' t.txt|grep x |cut -d',' -f 1`

do
sed -n '/<SQL>/,/<\/SQL>/H; /<SQL>/h; /\/SQL/{x;s/<SQL>\(.*[^\n]\)\n*<\/SQL>/\1/p;}' t.txt |sed -i 's/$a/trim($a)/g'

done
 
Old 10-16-2017, 01:49 PM   #2
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6051Reputation: 6051Reputation: 6051Reputation: 6051Reputation: 6051Reputation: 6051Reputation: 6051Reputation: 6051Reputation: 6051Reputation: 6051Reputation: 6051
there's no question.

also, please use code tags for code.
 
Old 10-16-2017, 04:31 PM   #3
tofino_surfer
Member
 
Registered: Aug 2007
Posts: 483

Rep: Reputation: 153Reputation: 153
In addition to there being no actual question there appear to be major typos and poor unreadable formatting.

SEL A.COLUMN_NMAE1,
A.FIND_REPLCAE

Should this be

SEL A.COLUMN_NAME1,
A.FIND_REPLACE

Also please put a space before and after each pipe | symbol for readability and use indenting.

Code:
#!/bin/bash

for a in `sed -n '/<SQL>/,/<\/SQL>/H; /<SQL>/h; /\/SQL/{x;s/<SQL>\(.*[^\n]\)\n*<\/SQL>/\1/p;}' t.txt | grep x | cut -d',' -f 1`
do
   sed -n '/<SQL>/,/<\/SQL>/H; /<SQL>/h; /\/SQL/{x;s/<SQL>\(.*[^\n]\)\n*<\/SQL>/\1/p;}' t.txt | sed -i 's/$a/trim($a)/g'
done
 
Old 10-17-2017, 12:33 AM   #4
spatil20
LQ Newbie
 
Registered: May 2015
Posts: 21

Original Poster
Rep: Reputation: Disabled
Simple requirement need to find the string A.FIND_REPLCAE column in <SQL>,</SQL> and replcae with trim(A.FIND_REPLCAE) in <SQL> </SQL> in XML.txt file

Input(evey time line numbers will change)
this is @12th line <SQL><SelectStatement modified=\\'1\\' type=\\'string\\'><![CDATA[
SEL A.COLUMN_NMAE1,
A.FIND_REPLCAE
FROM TABLE_NAME
\><Tables collapsed=\\'1\\'></Tables><Parameters collapsed=\\'1\\'></Parameters><Columns collapsed=\\'1\\'></Columns></SelectStatement><SQL>

this is @150 line <SQL><SelectStatement modified='1' type='string'><![CDATA[ SEL A.COLUMN_NMAE1, \(D)\(A) A.FIND_REPLCAE FROM TABLE_NAME]]><Tables collapsed='1'></Tables><Parameters collapsed='1'></Parameters><Columns collapsed='1'></Columns></SelectStatement></SQL>

my output:-
<SQL><SelectStatement modified=\\'1\\' type=\\'string\\'><![CDATA[
SEL A.COLUMN_NMAE1,
Trim(A.FIND_REPLCAE)
FROM TABLE_NAME
\><Tables collapsed=\\'1\\'></Tables><Parameters collapsed=\\'1\\'></Parameters><Columns collapsed=\\'1\\'></Columns></SelectStatement><SQL>

<SQL><SelectStatement modified='1' type='string'><![CDATA[ SEL A.COLUMN_NMAE1, \(D)\(A) Trim(A.FIND_REPLCAE) FROM TABLE_NAME]]><Tables collapsed='1'></Tables><Parameters collapsed='1'></Parameters><Columns collapsed='1'></Columns></SelectStatement></SQL>

Last edited by spatil20; 10-17-2017 at 12:34 AM.
 
Old 10-17-2017, 01:23 AM   #5
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 20,823

Rep: Reputation: 4004Reputation: 4004Reputation: 4004Reputation: 4004Reputation: 4004Reputation: 4004Reputation: 4004Reputation: 4004Reputation: 4004Reputation: 4004Reputation: 4004
Removed - posted across threads.

Last edited by syg00; 10-17-2017 at 02:16 AM.
 
Old 10-17-2017, 01:59 AM   #6
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6051Reputation: 6051Reputation: 6051Reputation: 6051Reputation: 6051Reputation: 6051Reputation: 6051Reputation: 6051Reputation: 6051Reputation: 6051Reputation: 6051
you need to use code tags for code.
==============================

Quote:
Originally Posted by spatil20 View Post
need to find the string A.FIND_REPLCAE column in <SQL>,</SQL> and replcae with trim(A.FIND_REPLCAE) in <SQL> </SQL> in XML.txt file
and why is the code snippet from post #1 not sufficient? show us.

but really i suspect that sed might not be the right tool for the job...
see here: https://www.linuxquestions.org/quest...on-4175615769/
 
Old 10-17-2017, 06:28 AM   #7
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 25,798

Rep: Reputation: 7745Reputation: 7745Reputation: 7745Reputation: 7745Reputation: 7745Reputation: 7745Reputation: 7745Reputation: 7745Reputation: 7745Reputation: 7745Reputation: 7745
Quote:
Originally Posted by spatil20 View Post
Simple requirement need to find the string A.FIND_REPLCAE column in <SQL>,</SQL> and replcae with trim(A.FIND_REPLCAE) in <SQL> </SQL> in XML.txt file

Input(evey time line numbers will change)
this is @12th line <SQL><SelectStatement modified=\\'1\\' type=\\'string\\'><![CDATA[
SEL A.COLUMN_NMAE1,
A.FIND_REPLCAE
FROM TABLE_NAME
\><Tables collapsed=\\'1\\'></Tables><Parameters collapsed=\\'1\\'></Parameters><Columns collapsed=\\'1\\'></Columns></SelectStatement><SQL>

this is @150 line <SQL><SelectStatement modified='1' type='string'><![CDATA[ SEL A.COLUMN_NMAE1, \(D)\(A) A.FIND_REPLCAE FROM TABLE_NAME]]><Tables collapsed='1'></Tables><Parameters collapsed='1'></Parameters><Columns collapsed='1'></Columns></SelectStatement></SQL>

my output:-
<SQL><SelectStatement modified=\\'1\\' type=\\'string\\'><![CDATA[
SEL A.COLUMN_NMAE1,
Trim(A.FIND_REPLCAE)
FROM TABLE_NAME
\><Tables collapsed=\\'1\\'></Tables><Parameters collapsed=\\'1\\'></Parameters><Columns collapsed=\\'1\\'></Columns></SelectStatement><SQL>

<SQL><SelectStatement modified='1' type='string'><![CDATA[ SEL A.COLUMN_NMAE1, \(D)\(A) Trim(A.FIND_REPLCAE) FROM TABLE_NAME]]><Tables collapsed='1'></Tables><Parameters collapsed='1'></Parameters><Columns collapsed='1'></Columns></SelectStatement></SQL>
Yes, parsing XML is fairly simple...which is why there are many Perl/CPAN modules specifically written to parse it. Have you examined any?
https://metacpan.org/search?q=xml

We're happy to help if you're stuck, but you've not posted anything you've written/done/tried on your own thus far. We aren't going to write your programs for you.
 
  


Reply

Tags
bash script $@, shell scripting


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to find and replace where the find is on two lines and replace on first line andrew777 Linux - General 9 08-05-2013 01:50 PM
[SOLVED] XML find and replace tags with grep or sed Darkhoros Programming 2 05-30-2013 02:59 PM
[SOLVED] Replace a string with sequential value in an xml juju_qa Programming 2 02-01-2012 05:32 AM
configure: error: could not find DocBook XML DTD V4.1.2 in XML catalog Fadoksi Linux - Software 1 07-16-2006 06:41 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:50 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration