sed & areas respectively sed & pyramiding


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed & areas respectively sed & pyramiding
# 1  
Old 10-07-2009
sed & areas respectively sed & pyramiding

Hello everyone,

i wonder if someone could give me an advice regarding the following problem using sed.

Given ist a structure as shown below:
<aaa>text1<b>text2</b>text3<c>text4</c>text5</aaa>

Now I want to change the outer tag from "aaa" to "new" and replace all tags inside the outer tags using underscores:
<new>text1_text2_text3_text4_text5</new>

The text has different length, the number and the names of the tags inside the outer tags differ.
Example:
<aaa>text1<b>text2</b>text3<c>text4</c>text5</aaa>
or
<aaa>text1111<bbb>text2122</bbb>texttttt3<cc>text4</cc>text5</aaa>

Is it possible to do this using sed?

Unfortunately I am new to sed and this is too diffucult for me... Smilie

Thx in advance and greetings from Germany

Marc
# 2  
Old 10-07-2009
Code:
echo '<aaa>text1<b>text2</b>text3<c>text4</c>text5</aaa>' | nawk -F'[<>]' -v OFS='' 'for(i=2; i<=NF; i+=2) $i=(i==2 || i==(NF-1))?"<new>":"_"; print}'

# 3  
Old 10-07-2009
Code:
# echo '<aaa>text1<b>text2</b>text3<c>text4</c>text5</aaa>' | nawk -F'[<>]'  '{for(i=2; i<=NF; i+=2) $i=(i==2 || i==(NF-1))?"<"(i==(2)?"":"/")"new>":"_"}1' OFS=''
<new>text1_text2_text3_text4_text5</new>


Last edited by danmero; 10-07-2009 at 02:31 PM.. Reason: Fix output
# 4  
Old 10-07-2009
good find, danmero - thanks for the fix.
# 5  
Old 10-07-2009
Thx for your answers!
I think i did not express the problem clearly enough.
Now a second attempt:
My attention is to change the attached file content.xml as shown in the picture below:

Image

The tag to be replaced (in the example of my first post "<aaa>") is not at the beginning of the structure. I did not mention it before because using sed this would not be a problem...

Regards

Marc
Image
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Echo "abc" | sed - r 's/a/&_&/

I want to know the working of & here step by step using sed command. (1 Reply)
Discussion started by: Preeti07
1 Replies

2. UNIX for Beginners Questions & Answers

sed pattern &

Hi, I can't seem to understand what the sed and & do.. Why is the set of numbers appear twice ? how the command really work ? echo "123 abc" | sed 's/*/& www &/' Output: 123 www 123 abc (3 Replies)
Discussion started by: uniran
3 Replies

3. Shell Programming and Scripting

Help with sed ( & )

I am searching for a specific pattern and replacing with ( ) for matched pattern .I am not getting the expected result ..... Here is the file cat test cool change Frinto Francis Frinto cool change Attitude /usr/bin sed 's/*/( & )/' test ( cool ) ( change ) ( )Frinto Francis... (2 Replies)
Discussion started by: frintocf
2 Replies

4. UNIX for Dummies Questions & Answers

awk & sed

Hi, Can anyone let me know the difference between awk and sed utilities in Unix? Many thanks. (2 Replies)
Discussion started by: venkatesht
2 Replies

5. Shell Programming and Scripting

Help, | ! & [ ^ in sed

Hi,everyone: I'm new to shell, and I'm confued with some script: ssum=`echo "$lsum" | sed 's|!|\\\\!|g'` line1=`echo "$line" | sed 's!\!\\\]!g'` line3=`echo "$line2" | sed 's!\&!\\\&!g'` sed "s^${line3}^${newline3}^g" ${TIER4FILE} > ${TMP_TIER4FILE} In the first three lines,... (5 Replies)
Discussion started by: mycoy
5 Replies

6. Shell Programming and Scripting

sed & awk

Hi. I'm going to learn scripting and i have the following topics on the list: sed, awk, shell scripting, perl. My question is, whehter i should learn sed and awk? Aren't this tools outdated? Although i see that GNU upgrade it's versions of these tools from time to time. And, the next... (9 Replies)
Discussion started by: kukuruku
9 Replies

7. Shell Programming and Scripting

New to Sed & Awk

How do I grab the first 10 characters of a line and append it to another empty file? (7 Replies)
Discussion started by: xgringo
7 Replies

8. Shell Programming and Scripting

sed, date and /&

Hi, $ echo 1 titi | sed -e "s/1/$(echo \&)/" 1 titi but $ echo 1 titi | sed -e "s/1/$(date \&)/" date: invalid date `&' titi how can i do for handle '\&' with date ? Thx (7 Replies)
Discussion started by: sncr24
7 Replies

9. Shell Programming and Scripting

cp & sed error

Hi, Below is a small piece of my Korn shell script - what i am trying to do is substitute all occurrences of the word given by the ${src} parameter with the word given by the ${dest} parameter in that particular textfile. But i get the errors below... for i in `ls... (19 Replies)
Discussion started by: n8575
19 Replies

10. Shell Programming and Scripting

sed & awk help...

I have a question. Take the following statement awk -F\| '{print $21}' testfile | sed 's/\//\\/g' > newfile This will grab the 21st column of a | delimited text file, replace the forward slashes "/" , with back slashes "\", and redirect the output newfile. Now, how do I get the output... (4 Replies)
Discussion started by: shimb0
4 Replies
Login or Register to Ask a Question