Copy and paste text inside a xml file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Copy and paste text inside a xml file
# 1  
Old 01-27-2017
Copy and paste text inside a xml file

I have a really big XML file. I need copy the value of one tag inside another one tag. I try to publish one example.

Code:
  <channel update="i" site="merge-xmltv" site_id="" xmltv_id="Rai 1">Rai 1</channel>
  <channel update="i" site="merge-xmltv" site_id="" xmltv_id="Rai 1 +2HD">Rai 1 +2HD</channel>
  <channel update="i" site="merge-xmltv" site_id="" xmltv_id="Rai 1 +1HD">Rai 1 +1HD</channel>
  <channel update="i" site="merge-xmltv" site_id="" xmltv_id="Rai 2">Rai 2</channel>
  <channel update="i" site="merge-xmltv" site_id="" xmltv_id="Rai 2 +2HD">Rai 2 +2HD</channel>
  <channel update="i" site="merge-xmltv" site_id="" xmltv_id="Rai 2 +1HD">Rai 2 +1HD</channel>

I need this file will be:

Code:
  <channel update="i" site="merge-xmltv" site_id="Rai 1" xmltv_id="Rai 1">Rai 1</channel>
  <channel update="i" site="merge-xmltv" site_id="Rai 1 +2HD" xmltv_id="Rai 1 +2HD">Rai 1 +2HD</channel>
  <channel update="i" site="merge-xmltv" site_id="Rai 1 +1HD" xmltv_id="Rai 1 +1HD">Rai 1 +1HD</channel>
  <channel update="i" site="merge-xmltv" site_id="Rai 2" xmltv_id="Rai 2">Rai 2</channel>
  <channel update="i" site="merge-xmltv" site_id="Rai 2 +2HD" xmltv_id="Rai 2 +2HD">Rai 2 +2HD</channel>
  <channel update="i" site="merge-xmltv" site_id="Rai 2 +1HD" xmltv_id="Rai 2 +1HD">Rai 2 +1HD</channel>

I tried to use the command sed

Code:
sed -i 's/\(.*xmltv_id="\)\(.*\)\(">.*site_id="\)\(**\)\(" xmltv_id.*\)/\1\2\3\2\5/' WebGrab++.config.xml

but something is wrong...

I am using cygwin and perl is not really supported, thank you if someone can help me!

Last edited by vgersh99; 01-27-2017 at 05:45 PM.. Reason: fixed code tags
# 2  
Old 01-27-2017
YMMV:
Code:
awk -F'"' '{$(NF-3)=$(NF-1)}1' OFS='"' myFile

This User Gave Thanks to vgersh99 For This Post:
# 3  
Old 01-28-2017
Can you explain me the command? I recive one error:

awk: cmd. line:1: (FILENAME=web.xml FNR=2) fatal: attempt to access field -2

I can't understand the reason, I never used awk Smilie, if you explain me I think I can fix Smilie Smilie
# 4  
Old 01-28-2017
Quote:
Originally Posted by Tapiocapioca
Can you explain me the command? I recive one error:

awk: cmd. line:1: (FILENAME=web.xml FNR=2) fatal: attempt to access field -2

I can't understand the reason, I never used awk Smilie, if you explain me I think I can fix Smilie Smilie
Assuming that you get the above diagnostic when running the script vgersh99 suggested:
Code:
awk -F'"' '{$(NF-3)=$(NF-1)}1' OFS='"' myFile

it is telling us that line number 2 in your input file (the one named web.xml) does not contain any double-quote (") characters. And, since the sample input that you showed us had four pairs of double-quotes on every input line, it wasn't prepared to handle input in a different format.

If you can't be bothered to accurately describe the format of the input data your script will be processing, we have to assume that you will be able to modify any suggestions provided to weed out (or otherwise process) lines in your input file(s) that do not match the format of the data you said you wanted to process.
This User Gave Thanks to Don Cragun For This Post:
# 5  
Old 01-28-2017
Quote:
Originally Posted by Don Cragun
it is telling us that line number 2 in your input file (the one named web.xml) does not contain any double-quote (") characters. And, since the sample input that you showed us had four pairs of double-quotes on every input line, it wasn't prepared to handle input in a different format.

If you can't be bothered to accurately describe the format of the input data your script will be processing, we have to assume that you will be able to modify any suggestions provided to weed out (or otherwise process) lines in your input file(s) that do not match the format of the data you said you wanted to process.
Mmmm...
After your explanation is really more clear. But push me to ask again.
I am asking to use one command like sed or awk because before I used a different command and I am sure was right, the command was:

Code:
xml ed --inplace -u "//channel/@site_id" -x "string(../@xmltv_id)" WebGrab++.config.xml

Using it, I had a similar error like now.
The error is:

Code:
WebGrab++.config.xml:2.2: Extra content at the end of the document

So I am thinking the errors I am having are not about the commds I am giving (awk or xmlstarlet) but are about the commands I gave before. The file is really big and the original format is like this:

Code:
<?xml version="1.0"?>
<settings>
.
.
.
.
<!--line 136 -->
<!--01-->
    <channel update="i" site="merge-xmltv-utc" site_id="" xmltv_id="Rai 1">Rai 1</channel>          <!-- Rai 1 -->
    <channel offset="2" same_as="Rai 1" xmltv_id="Rai 1 +2HD">Rai 1 +2HD</channel>          <!-- Rai 1 +2 HD -->
    <channel offset="1" same_as="Rai 1" xmltv_id="Rai 1 +1HD">Rai 1 +1HD</channel>          <!-- Rai 1 +1 HD-->
.
.
.
</settings>

If I apply both command (xmlstarlet or awk) on the original file they are working, but is not what I want. To fix the document and have the document like I published at the first post I need use some commands before, the commands are:
Code:
# Delete everything between "<channel" and "xmltv_id"
sed -ri 's/(    <channel )(.*)(xmltv_id)/\1\3/g' WebGrab++.config.xml
# Add after "<channel" the fields "<channel update="i" site="merge-xmltv" site_id="" "
sed -i 's/    <channel /    <channel update="i" site="merge-xmltv" site_id="" /g'  WebGrab++.config.xml
# Delete lines from 1 to 136
sed -i '1,136d' WebGrab++.config.xml
# Delete last line
sed -i '$d' WebGrab++.config.xml
# Delete all lines contain comments
sed -i '/^</d' WebGrab++.config.xml
# Delete all empty lines
sed -i '/^ *$/d' WebGrab++.config.xml
# Delete all comments till the end of the line
sed -i 's/<!--.*//' WebGrab++.config.xml
# Delete all blank space or tab at the end of the line
sed -i 's/[[:blank:]]*$//' WebGrab++.config.xml

Afer I applied all this procedure, the file look like damage, like sed did something wrong after the second line.

Do you have any idea what's happen?
# 6  
Old 01-28-2017
It is not much clearer to me WHAT you really need in the end, but Don Cragun's analysis proves right when seeing your second sample input file.
You might want to safeguard your script by adding some tests for applicability (or justification) of the modifications:

Code:
awk -F'"' '/channel update/ && !$6 {$(NF-3)=$(NF-1)}1' OFS='"' file

Adding some or all of your "damage producers" is not a problem once it is known which should be included
This User Gave Thanks to RudiC For This Post:
# 7  
Old 01-29-2017
Quote:
Originally Posted by RudiC
It is not much clearer to me WHAT you really need in the end, but Don Cragun's analysis proves right when seeing your second sample input file.&lt;br /&gt;<br />
You might want to safeguard your script by adding some tests for applicability (or justification) of the modifications:&lt;br /&gt;<br />
&lt;br /&gt;<br />
Code:
awk -F'&amp;quot;' '/channel update/ &amp;amp;&amp;amp; !$6 {$(NF-3)=$(NF-1)}1' OFS='&amp;quot;' file

Adding some or all of your &amp;quot;damage producers&amp;quot; is not a problem once it is known which should be included
When I back home, I will attach the file to be more clear.

---------- Post updated 01-29-17 at 04:12 AM ---------- Previous update was 01-28-17 at 08:26 AM ----------

I found the solution. The command

Code:
sed -ri 's/(    <channel )(.*)(xmltv_id)/\1\3/g' WebGrab++.config.xml

damage the file and after awk can't replace the fields. Using the command:

Code:
sed -i -e 's/\(    <channel \).*\(xmltv_id\)/\1\2/ 'WebGrab++.config.xml

everything works fine!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

How to write in other language in text/xml file by reading english text/xml file using C++?

Hello Team, I have 2 files.one contains english text and another contains Japanese. so i have to read english text and replace the text with Japanesh text in third file. Basically, I need a help to write japanese language in text/xml file.I heard wstring does this.Not sure how do i write... (2 Replies)
Discussion started by: SA_Palani
2 Replies

2. Shell Programming and Scripting

Bash copy and paste text in file from one position to another

Hi I have a text file with lines beginning with 71303, 71403, 71602, I need to copy the 10 digit text at position 30 on lines beginning with 71303 (5500011446) to position 99 on every line beginning with 71602 (see example below), There may be many 71303 lines but I need the text copying to... (2 Replies)
Discussion started by: firefox2k2
2 Replies

3. Shell Programming and Scripting

Replace text inside XML file based on condition

Hi All, I want to change the name as SEQ_13 ie., <Property Name="Name">SEQ_13</Property> when the Stage Type is PxSequentialFile ie., <Property Name="StageType">PxSequentialFile</Property> :wall: Input.XML <Main> <Record Identifier="V0S13" Type="CustomStage" Readonly="0">... (3 Replies)
Discussion started by: kmsekhar
3 Replies

4. Shell Programming and Scripting

Copy and Paste Columns in a Tab-Limited Text file

I have this text file with a very large number of columns (10,000+) and I want to move the first column to the position of the six column so that the text file looks like this: Before cutting and pasting ID Family Mother Father Trait Phenotype aaa bbb ... (5 Replies)
Discussion started by: evelibertine
5 Replies

5. Shell Programming and Scripting

Loop through text file > Copy Folder > Edit XML files in bulk?

I have a text file which contains lines in this format - it contains 105 lines in total, but I'm just putting 4 here to keep it short: 58571,east_ppl_ppla_por 58788,east_pcy_hd_por 58704,east_pcy_ga_por 58697,east_pcy_pcybs_por It's called id_key.txt I have a sample folder called... (9 Replies)
Discussion started by: biscuitcreek
9 Replies

6. Homework & Coursework Questions

copy files inside a text file

Hi Guys , I am new to this and Hi to all ,Need your help I am trying to copy Files which are inside file.txt The files inside file.txt are inthe below order file1.log file2.log file3.log ....... I want to copy these files to an output Directory , Please help (1 Reply)
Discussion started by: hc17972
1 Replies

7. Homework & Coursework Questions

copy files inside a text file

Hi Guys , I am new to this and Hi to all ,Need your help I am trying to copy Files which are inside file.txt The files inside file.txt are inthe below order file1.log file2.log file3.log ....... I want to copy these files to an output Directory , Please help (1 Reply)
Discussion started by: hc17972
1 Replies

8. Solaris

Copy and paste text from a word document into a txt file in vi

Hello, Can anybody please tell me how we can copy and paste text from a word document into a text file that we are editing in vi? Is it possible to do that while we are editing the text file in vi in insert mode? Thanks, (3 Replies)
Discussion started by: Pouchie1
3 Replies

9. UNIX for Dummies Questions & Answers

Copy/Paste text as commands in AIX

Hello, I'm absolutely new to this world... but I've a problem with a terminal connected via PuTTY (or Termlite) to an AIX 5.1 application. The problem: I need to paste from clipboard a text containing both input text strings and special keys as ESC, Arrows and so on, to execute in the AIX... (1 Reply)
Discussion started by: Daniele11
1 Replies

10. Shell Programming and Scripting

how to replace a text inside a file based on a xml key

<c-param> <param-name>Number</param-name> <param-value>22</param-value> <description>my house number</description> </c-param> <c-param> <param-name>Address</param-name> ... (4 Replies)
Discussion started by: reldb
4 Replies
Login or Register to Ask a Question