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 08-31-2017, 04:33 AM   #1
Effman
LQ Newbie
 
Registered: Mar 2017
Location: Hamburg
Distribution: Debian
Posts: 17

Rep: Reputation: Disabled
Question xml bash script - copy element and change the name


Hi everyone,
I have an xml file with a list of articles. To be able to import it i have to change names and delete some entries. That part has been no problem (did it using sed).
Now I'm stuck because i have to duplicate an entry and change the name.

The original file looks like this:

Code:
  <PRODUKT>
    <ARTIKELNUMMER>00001</ARTIKELNUMMER>
    <PREIS>100.00</PREIS>
  </PRODUKT>
  <PRODUKT>
    <ARTIKELNUMMER>00002</ARTIKELNUMMER>
    <PREIS>200.00</PREIS>
  </PRODUKT>
  <PRODUKT>
    <ARTIKELNUMMER>00003</ARTIKELNUMMER>
    <PREIS>300.00</PREIS>
  </PRODUKT>
I have to duplicate the entry "Artikelnummer" and change the name, value stays the same.
So the result have to look like this:

Code:
  <PRODUKT>
    <ARTIKELNUMMER>00001</ARTIKELNUMMER>
    <NEWENTRY>00001</NEWENTRY>
    <PREIS>100.00</PREIS>
  </PRODUKT>
  <PRODUKT>
    <ARTIKELNUMMER>00002</ARTIKELNUMMER>
    <NEWENTRY>00002</NEWENTRY>
    <PREIS>200.00</PREIS>
  </PRODUKT>
  <PRODUKT>
    <ARTIKELNUMMER>00003</ARTIKELNUMMER>
    <NEWENTRY>00003</NEWENTRY>
    <PREIS>300.00</PREIS>
  </PRODUKT>
Whenever i try to grab the value, put it in a variable and insert it, i endet up adding one line at the bottom of the file. It's my first time editing xml, hope someone can help me.

The script is supposed to run on an Ubuntu Server and if possible I'd like to use built in commands without installing too much stuff.

Thanks in advance!

Effman
 
Old 08-31-2017, 04:38 AM   #2
AwesomeMachine
LQ Guru
 
Registered: Jan 2005
Location: USA and Italy
Distribution: Debian testing/sid; OpenSuSE; Fedora; Mint
Posts: 5,524

Rep: Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015
I would do it the way you have, but then take the line out at the end if you don't like it. I don't think it would do any harm to have a blank line at the end of the file.
 
Old 08-31-2017, 04:58 AM   #3
Effman
LQ Newbie
 
Registered: Mar 2017
Location: Hamburg
Distribution: Debian
Posts: 17

Original Poster
Rep: Reputation: Disabled
I'm not sure how this should work? When i import the "result example" 3 products with 3 values each will be imported.
When i add one copied line at the bottom it would look like this:

Code:
  <PRODUKT>
    <ARTIKELNUMMER>00001</ARTIKELNUMMER>
    <PREIS>100.00</PREIS>
  </PRODUKT>
  <PRODUKT>
    <ARTIKELNUMMER>00002</ARTIKELNUMMER>
    <PREIS>200.00</PREIS>
  </PRODUKT>
  <PRODUKT>
    <ARTIKELNUMMER>00003</ARTIKELNUMMER>
    <PREIS>300.00</PREIS>
  </PRODUKT>
<NEWENTRY>00001</NEWENTRY>
Import would only recognize 3 products with 2 values each and one useless line. Or am i wrong?
 
Old 08-31-2017, 08:17 AM   #4
allend
LQ 5k Club
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware64-15.0
Posts: 6,461

Rep: Reputation: 2797Reputation: 2797Reputation: 2797Reputation: 2797Reputation: 2797Reputation: 2797Reputation: 2797Reputation: 2797Reputation: 2797Reputation: 2797Reputation: 2797
Awk is your friend here.
Code:
awk '/ARTIKELNUMMER/ {print; print gensub(/ARTIKELNUMMER/,"NEWENTRY","g"); next}; {print}'
 
1 members found this post helpful.
Old 08-31-2017, 09:11 AM   #5
Effman
LQ Newbie
 
Registered: Mar 2017
Location: Hamburg
Distribution: Debian
Posts: 17

Original Poster
Rep: Reputation: Disabled
Hi allend,
thanks for your answer, does exactly what i need! It can be so easy...!
 
  


Reply


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
XML to CSV with a resursive element to it neverton Programming 9 06-07-2012 08:58 PM
XML- Sed Bash script... Help! juampal Programming 8 09-07-2011 12:58 AM
bash script to find the difference between the 4th element of each line in 2 files dontbugmeplz Programming 6 07-21-2008 09:05 AM
Parse XML in bash script MikeyCarter Linux - Software 1 02-16-2007 02:19 PM
XML Schema element references Omni Programming 0 09-22-2006 03:07 PM

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

All times are GMT -5. The time now is 02:05 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