Replacing tags


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replacing tags
# 8  
Old 09-27-2012
My requirement is little bit different. Smilie . Sorry for asking so much.

if you see the output of mine

Output:
Value11|value12|value13|value14
Value21||value23|

All line have exactly same number of '|'.
# 9  
Old 09-27-2012
try this...
Code:
awk -F "[<>]" '{ for(i=2;i<=NF;i+=4){a++;(gsub("[a-z]","",$i));if($i ~ a){if(s){s=s"|"$(i+1)}else{s=$(i+1)}}else{s=s"||"$(i+1)"|"}}}{print s;s="";a=0}' file

This User Gave Thanks to pamu For This Post:
# 10  
Old 09-27-2012
Well .. I must say it's working good but only till certain extent ...
For example ...

For the file having
<tag1> Value11</tag1><tag2>value12</tag2><tag3>value13</tag3><tag4>value14</tag4>
<tag1>Value21</tag1><tag3>value23</tag3>
<tag1> Value11</tag1><tag2>value12</tag2><tag3>value13</tag3><tag4>value14</tag4>
<tag1> Value11</tag1><tag2>value12</tag2><tag3>value13</tag3><tag4>value14</tag4>
<tag1> Value11</tag1><tag2>value12</tag2>

Output is coming as


Value11|value12|value13|value14
Value21||value23|
Value11|value12|value13|value14
Value11|value12|value13|value14
Value11|value12

So each line does not have same number of pipes Smilie.
# 11  
Old 09-27-2012
Try the following:
Code:
awk -F "(<[/]*tag|>)" '
{       for(i = 2; i <= NF; i += 4) {
                o[NR,$i] = $(i + 1)
                if($i > maxf) maxf = $i
        }
}
END {   for(i = 1; i <= NR; i++)
                for(j = 1; j <= maxf; j++)
                        printf("%s%s", o[i,j], j == maxf ? "\n" : "|")
}' input

If the file input contains:
Code:
<tag1> Value11</tag1><tag2>value12</tag2>
<tag10> Value10</tag10><tag2>value12</tag2>
<tag5>field5</tag5><tag4>field4</tag4><tag3>field3</tag3><tag2>field2</tag2><tag1>field1</tag1><tag7>field7, no 6</tag7><tag10>no 8 or 9; this is 10</tag10>

it will produce the following output:
Code:
 Value11|value12||||||||
|value12|||||||| Value10
field1|field2|field3|field4|field5||field7, no 6|||no 8 or 9; this is 10

These 2 Users Gave Thanks to Don Cragun For This Post:
# 12  
Old 09-28-2012
Awesome Smilie.

Can you change it a little bit? We don't get the tags like tag1, tag2 Smilie

So what we get it is
1. One parameter.
Exampls: All tags="Tag1,Tag2,tag3,.....,Tagn"

Now from it we will get the informations like
1. how many tags are there
2. What is the exact sequence of the tags. Sequence will never change in the file. So for above example tag1=3 will never come before Tag2.
# 13  
Old 09-28-2012
just change this to Don's solution.....(awesome code by Don..Smilie)

Code:
awk -F "(<[/]*Tag|>)" '

This User Gave Thanks to pamu For This Post:
# 14  
Old 09-28-2012
Code:
cat file.txt

<tag1> Value11</tag1><tag2>value12</tag2><tag3>value13</tag3><tag4>value14</tag4>
<tag1>Value21</tag1><tag3>value23</tag3>
<tag1> Value11</tag1><tag2>value12</tag2><tag3>value13</tag3><tag4>value14</tag4>
<tag1> Value11</tag1><tag2>value12</tag2><tag3>value13</tag3><tag4>value14</tag4>
<tag1> Value11</tag1><tag2>value12</tag2>


tags='tag1,tag2,tag3,tag4'

awk -v tags="$tags" 'BEGIN{max=split(tags,t,",")}
{p=""
for(i=1;i<=max;i++)
{
 patt="<" t[i] ">[^<]*</" t[i] ">"
 if(match($0,patt))
 {
  v=substr($0,RSTART,RLENGTH)
  gsub(/<[^>]+>/,"",v)
 }
 else
  v=""
 p=p v "|"
}
sub(/[|]$/,"",p)
print p
}' file.txt

 Value11|value12|value13|value14
Value21||value23|
 Value11|value12|value13|value14
 Value11|value12|value13|value14
 Value11|value12||

This User Gave Thanks to elixir_sinari For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Replacing HTML tags with sed

Ok, so this is stupid simple, and I know I am going to feel like an idiot when I get help. I am altering a HTML report that has contraband in it so that the links to said contraband and the images are not shown. The link/img pairs are in the form of : <a... (5 Replies)
Discussion started by: twjolson
5 Replies

2. Shell Programming and Scripting

Problem while replacing text between two tags

Hi, I have a file i which i want to replace {gIntegraHost} to {gTestRunHost} between <TCPSampler and curl only. I am firing below command sed '1,/<TCPSampler/!{ /curl/,/<TCPSampler/!s/{gIntegraHost}/{gTestRunHost}/; }' file1 But only on instance is getting replaced not all. Any idea how... (6 Replies)
Discussion started by: abhitanshu
6 Replies

3. Shell Programming and Scripting

Replacing part of XML code inside comment tags

Hello! I'd like to modify custom values in a XML config file between comment tags using bash script. <feature> <keyboardshortcut>C-m</keyboardshortcut> <option1>disabled</option2> <option2>enabled</option2> </feature> <!-- bash script features START --> <feature> ... (2 Replies)
Discussion started by: prism1
2 Replies

4. Shell Programming and Scripting

Replacing number between xml tags with ksh shell script

Hallo, im basically a complete noob on shell scripting and im trying to replace or rather add 1 to a number between xml tags. The xml basically has a tag somewhere that looks like this: <tag>12345678901234</tag> Now i want to replace the number between the tags. And i want the file to... (6 Replies)
Discussion started by: Demoric
6 Replies

5. Shell Programming and Scripting

Replacing variable values in html tags

Hi please help me with this . I have a file test.txt with following content $cat test.txt <td>$test</td> <h2>$test2</h2> and I have a ksh with following content $cat test.ksh #!/bin/ksh test=3 test2=4 while read line do echo $line done < test.html I am expecting the output as (4 Replies)
Discussion started by: panduandpavan
4 Replies

6. Shell Programming and Scripting

Replacing text between Tags, with output from a complex command

The Problem I have n files all named "conf.cfg", each of which contain a line that reads: "MyVar=XXX", where XXX can be any value. I have a second file called report.xml, that might read: <Reports> <Report>Report1</Report> <Report>Report2</Report> <Report>Report3</Report> </Reports> I... (1 Reply)
Discussion started by: Flang
1 Replies

7. Shell Programming and Scripting

Replacing the last record in xml with different tags

I have special requirement, my system provided the xml file as below(available xml file) and I need to convert it as below desired xml file. is it possible thru shell scripts or awk? What I need is : my available xml contains number of records with tags <RevenueAmounts>, the last of record is... (6 Replies)
Discussion started by: LinuxLearner
6 Replies

8. Shell Programming and Scripting

searching & replacing/removing only certain HTML tags

I generally save a lot of web pages for reading offline which works out great for school. Now I have to spend a lot of time on the bus and I am looking for the best way to read some of these webpages using my Nokia 7610. I have uploaded the files to my phone, but they are deadly deadly slow to... (2 Replies)
Discussion started by: naphelge
2 Replies

9. Shell Programming and Scripting

In PHP replacing text between TAGS & URI information in Title Tag

Hi, what I am trying to do in PHP, is to replace the Title. I need some of the URL information inside aswell depending on the domain. The title is always different so I need to store it in a variable, put the url info like described below in front of it. Here is an example how it should... (0 Replies)
Discussion started by: lowmaster
0 Replies

10. Shell Programming and Scripting

help in replacing ??

hi all i have input file like this abc.txt filename.out: <TAB>ABC<TAB>9 <TAB>AKC<TAB>1 filename1.out: <TAB>XYZ<TAB>1 <TAB>XYN<TAB>4 and i am trying to replace \n\t with \t so that output will be like this: filename.out:<TAB>ABC<TAB>9<TAB>AKC<TAB>1... (5 Replies)
Discussion started by: zedex
5 Replies
Login or Register to Ask a Question