sed: replace same number of characters between tags
I am having trouble getting the command right for replacing a series of characters between two tags with spaces with the aid of sed. The idea is the following:
Replace Quote:
Quote:
I have an HTML ASCII art file, which I would like to "convert" with sed to a simple text file that I could use in other areas without the tags. I'd have to remove all tags and add the appropriate new lines, but with sed that is a piece of cake. The only problem that remains is the "between-tags" thingy. Thanks! |
How about posting what you have tried?
One way to approach this is with addressing. Example: sed '/TAG ONE/s/\**/ /g' Translation: For all lines containing the string "TAG ONE", replace a string containing any number of "*"s with a single space. Do this for all occurrences on the same line. Take out the extra "*" to replace each "*" with a space. Best SED tutorial here: http://www.grymoire.com/Unix/Sed.html |
I think perl is better for this multi-line stuff but I may be ignorant, I never use sed anyway:
Code:
#!/usr/bin/perl -w Code:
<TAG ONE>xxxxx</TAG ONE> There's almost certainly a similar way to do it with some shell code (using sed) tho. But you won't get it from me ;) |
Sudden after thought -- if you don't want to use a script this will work from the command line:
Code:
perl -pi'orig_*' -e 's/\*/x/g if /^<TAG ONE>/' yourfile.txt |
OK, thanks. Sometimes there are several different tag environments on one line and then it changes all * into spaces instead of the ones between <TAG ONE> and </TAG ONE> only.
|
Using GNU sed
Code:
sed ':a s/\(<TAG ONE> *\)\*/\1 /;ta' infile > outfile |
Thanks, I would have never come up with the last one! Many thanks.
|
All times are GMT -5. The time now is 05:12 AM. |