replace characters with spaces between tag


 
Thread Tools Search this Thread
# 1  
replace characters with spaces between tag

I have a file where in some records are having the <Start> and <End> tag. There is data before the start tag , between the tages and after the End tag. I want to replace everything between the start & end tag with equivalent spaces.

Input File

Code:
afsdfaksddfs<start>12678<end>sgdfgdfsf
afsdfaksddfs<start>4678<end>sgdfgdfsf
afsdfaksddfs<start>123678<end>sgdfgdfsf
afsdfaksddfs<start>12345678asdf<end>sgdfgdfsf

Output File

Code:
afsdfaksddfs<start>     <end>sgdfgdfsf
afsdfaksddfs<start>    <end>sgdfgdfsf
afsdfaksddfs<start>      <end>sgdfgdfsf
afsdfaksddfs<start>            <end>sgdfgdfsf

Thanks in advance for the help.

Last edited by Yogesh Sawant; 05-13-2010 at 07:23 AM.. Reason: added code tags
# 2  
Code:
sed 's/<start>.*<end>/<start> <end>/' file

# 3  
equivalent spaces required

If there are 5 characters between <start> and <end> tag I want them to be replaced with 5 spaces. Your solution is almost there but is replacing the content with single space.
# 4  
Try...
Code:
awk -F">" '{for(i=0;++i<=length($2);){
 if(substr($2,i,1)=="<"){
   gsub(substr($2,i),"",$2)
   $2=$2 "<end";print $0;next} 
   gsub(substr($2,i,1)," ",$2)}}1' OFS=">" infile

# 5  
minor twek in requirement

As per requirement I don't want even the start and end tag. It shlould also be replaced with equivalent spaces.

Input File

Code:
6572537265325476234START1928 oakland END734678634895634895

Output File

Code:
6572537265325476234 734678634895634895


Last edited by Scott; 05-13-2010 at 08:14 AM.. Reason: Code tags, please...
# 6  
please try the above code and also put your sample file in code tags for better reading.

Last edited by malcomex999; 05-13-2010 at 05:14 AM..
# 7  
MySQL

Quote:
Originally Posted by varunrbs
If there are 5 characters between <start> and <end> tag I want them to be replaced with 5 spaces. Your solution is almost there but is replacing the content with single space.
Code:
# cat file1
afsdfaksddfs<start>12678<end>sgdfgdfsf
afsdfaksddfs<start>4678<end>sgdfgdfsf
afsdfaksddfs<start>123678<end>sgdfgdfsf
afsdfaksddfs<start>12345678asdf<end>sgdfgdfsf

Code:
# ./changespace
afsdfaksddfs<start>    <end>sgdfgdfsf
afsdfaksddfs<start>   <end>sgdfgdfsf
afsdfaksddfs<start>     <end>sgdfgdfsf
afsdfaksddfs<start>           <end>sgdfgdfsf

Code:
# cat changespace
 
#!/bin/bash
for i in `sed 's/.*<start>\(.*\)<end>.*/\1/' file1 `
   do
     space=${#i}
     mycaracter=" "
    while [[ $(( space -= 1 )) -gt -1 ]]
         do
           a=$a$mycaracter
         done
     sed -i "s/<start>$i<end>/<start>$a<end>/" file1 > /dev/null
     a=""
   done
cat file1


Last edited by ygemici; 05-13-2010 at 10:27 AM.. Reason: tag editing
 

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find a tag with data and replace its data in another tag

Hi I have one file, :16R::GENL :20C::RELA//SET//ABC123456 :22F::XYZYESR :20C::MITI//NETT/QWERTY12345 :16S::GENL :16R::GENL :20C::RELA//SET//XYZ23456 :22F::XYZYESR :16S::GENL The requirement is, if :20C::MITI// is present in any block, then replace the data of :20C::MITI// in... (8 Replies)
Discussion started by: Soumyadip Dutta
8 Replies

2. Shell Programming and Scripting

XML files with spaces in the tag name, parse & display?

Greetings all, I have an XML file that is being generated from my application, here is a sample of the first tag (That I am trying to remove and display in a list..) Example- <tag one= "data" data="1234" updateTime="1300"> <tag one= "data1" data="1234" updateTime="1300"> <tag... (5 Replies)
Discussion started by: jeffs42885
5 Replies

3. Shell Programming and Scripting

sed search and replace after xml tag

Hi All, I'm new to sed. In following XML file <interface type='direct'> <mac address='52:54:00:86:ce:f6'/> <source dev='eno1' mode='bridge'/> <model type='virtio'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/> </interface> ... (8 Replies)
Discussion started by: varunrapelly
8 Replies

4. How to Post in the The UNIX and Linux Forums

How to replace value of password tag in xml with blanks when special characters are there?

Hi All, I am trying to replace the values inside <password> tag in an xml file but it doesn't replace certain passwords: For eg: Server/home/sperinc>cat TextXML.txt <appIds> <entry name="AccountXref"> <type id="ldap"> <realm>nam</realm> ... (7 Replies)
Discussion started by: saroopkris85
7 Replies

5. Shell Programming and Scripting

Need to replace XML TAG

As per the requirement I need to replace XML tag with old to new on one of the XML file. Old<com : DEM>PHI</com : DEM> New<com : DEM>PHM</com : DEM> Please someone provide the sed command to replace above mentioned old XML tag with new XML tag (2 Replies)
Discussion started by: siva83
2 Replies

6. Shell Programming and Scripting

Replace special characters with Escape characters?

i need to replace the any special characters with escape characters like below. test!=123-> test\!\=123 !@#$%^&*()-= to be replaced by \!\@\#\$\%\^\&\*\(\)\-\= (8 Replies)
Discussion started by: laknar
8 Replies

7. AIX

Replace all TAB characters with white spaces

Dear Gurus Can you please advise me on how to Replace all TAB characters with white spaces in a text file in AIX? Either using vi or any utilities (2 Replies)
Discussion started by: tenderfoot
2 Replies

8. Shell Programming and Scripting

Replace several numbers with respective tag and make a single file

Dear All, I have a final output files as 736645|0| 13879|1| 495563|10| 127933|14| 4975|16| 49038|6| 53560|7| 135115|8| 178857|9| Now I want to replace second column with respective tag as per the value (4 Replies)
Discussion started by: jojo123
4 Replies

9. UNIX for Dummies Questions & Answers

Replace only first found white spaces with some other characters

Anybody can help me How can I replace only four first white spaces with , or any other characters aaaa 08/31/2004 08/31/2009 permanent Logical Processors in System: 64 bedad 08/16/2001 08/15/2011 permanent Logical Processors in System: 64 badnv14 05/31/2008 05/30/2013 permanent Logical... (5 Replies)
Discussion started by: pareshan
5 Replies

10. Shell Programming and Scripting

Add spaces between Characters

I need to add spaces in between characters in a string variable. Is there a shortcut? I know you can remove the spaces with sed, but does sed have a way to add them? Example: I have: DATA01 I want it to be: D A T A 0 1 What I have done so far is to create a function... (4 Replies)
Discussion started by: heyindy06
4 Replies

Featured Tech Videos