LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-25-2012, 05:39 AM   #1
corcodelagaze
LQ Newbie
 
Registered: Feb 2012
Posts: 6

Rep: Reputation: Disabled
sed edit the line above a regex expression


hello ,
for the last 2 days i`m dealing with teh following problem
i have a huge xml files and i want change the line that is above the finded regex

for example
xml file looks like
<grupo>AUDIO</grupo>
<familia>Tarjeta de Sonido Interna</familia>
<marca>CREATIVE</marca>

and it should look like
<grupo>Componentpc</grupo>
<familia>Tarjeta de Sonido Interna</familia>
<marca>CREATIVE</marca>

i did
cat 000615.xml |sed -n '/Tarjeta de Sonido Interna/{g;1!p;};h'
<grupo>AUDIO</grupo>
<grupo>AUDIO</grupo>
<grupo>AUDIO</grupo>
<grupo>AUDIO</grupo>
<grupo>AUDIO</grupo>
and extracted the line above but now i`m stucked `cause i don`t know how to change the line
help! boss is killing me
 
Old 02-25-2012, 05:48 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

Assuming the given example is correct, here's one way of doing just that:
Code:
grep -B2 "<marca>CREATIVE</marca>" 000615.xml | sed '/>AUDIO</s/AUDIO/Componentpc/'
Test run:
Code:
$ cat 000615.xml
<grupo>AUDIO</grupo>
<familia>Tarjeta de Sonido Interna</familia>
<marca>CREATIVE</marca>
<grupo>AUDIO</grupo>
<familia>Tarjeta de Sonido Interna</familia>
<marca>CREATIVE</marca>
<grupo>AUDIO</grupo>
<familia>Tarjeta de Sonido Interna</familia>
<marca>CREATIVE</marca>

$ grep -B2 "<marca>CREATIVE</marca>" infile | sed '/>AUDIO</s/AUDIO/Componentpc/'
<grupo>Componentpc</grupo>
<familia>Tarjeta de Sonido Interna</familia>
<marca>CREATIVE</marca>
<grupo>Componentpc</grupo>
<familia>Tarjeta de Sonido Interna</familia>
<marca>CREATIVE</marca>
<grupo>Componentpc</grupo>
<familia>Tarjeta de Sonido Interna</familia>
<marca>CREATIVE</marca>
Hope this helps.
 
Old 02-25-2012, 05:57 AM   #3
corcodelagaze
LQ Newbie
 
Registered: Feb 2012
Posts: 6

Original Poster
Rep: Reputation: Disabled
the thing is that the file has thousands of lines

i tryed to raise the lines like this
grep -B323232332 "<familia>Tarjeta de Sonido Interna</familia>" 000615.xml | sed '/>AUDIO</s/AUDIO/Componentpc/' > aa.xml

but i see that it doesn`t do the work
cat 000615.xml |wc -l
75219
[root@ventadecoche script]# cat aa.xml |wc -l
30350
 
Old 02-25-2012, 06:01 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,
Quote:
Originally Posted by corcodelagaze View Post
the thing is that the file has thousands of lines

i tryed to raise the lines like this
grep -B323232332 "<familia>Tarjeta de Sonido Interna</familia>" 000615.xml | sed '/>AUDIO</s/AUDIO/Componentpc/' > aa.xml

but i see that it doesn`t do the work
cat 000615.xml |wc -l
75219
[root@ventadecoche script]# cat aa.xml |wc -l
30350
I'm not sure what the problem is..... The solution given works on small and big files.

Grep's -B2 option tells grep to also show the 2 lines before the line a hit is matched. The sed part checks those 3 lines and only modifies the line that holds AUDIO.

Please elaborate about the problem you are having.
 
Old 02-25-2012, 06:10 AM   #5
corcodelagaze
LQ Newbie
 
Registered: Feb 2012
Posts: 6

Original Poster
Rep: Reputation: Disabled
first of all thank you for your help !!!
i have a xml file whith thousands of lines and i need to change some lines
000615.xml looks like
<producto num="1267">
<codigo>CR AUDIGY SE OEM</codigo>
<codigo_2>-CR AUDIGY SE OEM</codigo_2>
<codigo_3>SB0570</codigo_3>
<codigo_4 />
<grupo>AUDIO</grupo>
<familia>Tarjeta de Sonido Interna</familia>
<marca>CREATIVE</marca>
<precio>14.68</precio>
<stock>68</stock>
<peso>0.00</peso>
<nombre>CREATIVE TARJETA DE SONIDO AUDIGY SE OEM PCI</nombre>
<descripcion />
<caracteristicas />
<imagen>http://www.sss.com/1.jpg</imagen>
</producto>
i need to change <grupo>AUDIO</grupo> into <grupo>componentspc</grupo> only where it finds <familia>Tarjeta de Sonido Interna</familia>
the logic scheme would look like
cat 000615.xml | find the lines <familia>Tarjeta de Sonido Interna</familia> and change the line obove it from <grupo>AUDIO</grupo> into

<grupo>componentspc</grupo>
 
Old 02-25-2012, 06:18 AM   #6
corcodelagaze
LQ Newbie
 
Registered: Feb 2012
Posts: 6

Original Poster
Rep: Reputation: Disabled
uhh oh i need to replace the line <grupo>AUDIO</grupo> into <grupo>Componentpc</grupo> in the file 000615.xml
 
Old 02-25-2012, 06:23 AM   #7
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

Give this a try:
Code:
sed '/AUDIO/{N;/Tarjeta de Sonido Interna/s/AUDIO/Componentspc/}' 000615.xml
This works on the small example you have given. It does assume that the line containing Tarjeta de Sonido Interna is always below the line containing AUDIO.

Hope this helps.
 
1 members found this post helpful.
Old 02-25-2012, 06:27 AM   #8
corcodelagaze
LQ Newbie
 
Registered: Feb 2012
Posts: 6

Original Poster
Rep: Reputation: Disabled
yupyy !!! that dit the job thank thank you !!!
 
Old 02-25-2012, 06:29 AM   #9
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Quote:
Originally Posted by corcodelagaze View Post
yupyy !!! that dit the job thank thank you !!!
You're welcome

BTW: Can you put up the [SOLVED] tag.
first post -> Thread Tools -> Mark this thread as solved
 
Old 02-25-2012, 08:52 AM   #10
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Alternate solution with xmlstarlet:
Code:
xml ed -u '//producto[familia="Tarjeta de Sonido Interna"]/grupo[.="AUDIO"]' -v componentspc 000615.xml
 
  


Reply



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
regex expression ksmatthews Linux - Software 5 01-11-2012 10:43 AM
[SOLVED] sed expression help bmorency Linux - Software 4 03-17-2010 07:24 AM
what regex expression replaces file extension using bash script jdavis2 Linux - General 5 01-14-2010 05:39 PM
[SOLVED] sed or awk help - need to remove text on each line before a regular expression kmkocot Linux - Newbie 15 10-30-2009 03:20 AM
regex with sed to process file, need help on regex dwynter Linux - Newbie 5 08-31-2007 05:10 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:28 AM.

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