Linux - NewbieThis 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
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.
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'
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
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>
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.