Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sed script from vi commands

Status
Not open for further replies.

jad

Programmer
Apr 7, 1999
1,195
GB
i have a few vi commands that i'd like converting to something that can run on a command line, something like sed'd be good ...

i've got an html doc that needs cleaning up ... someone elses i must say, i hand code mine, but he's got about 50 docs, and i was just thinking if there was an easier way than 'vi'ing each doc and typing in the following commands.

Code:
vi <doc>.htm (yuck, not even html)
:g/></s//>^M</g (yes that is ctrl-v, ctrl-m)
:g/<[^>]*$/j (repeated lots of times ... j joins lines and puts a space before each join, although i don't need the space)
:g/<\/P[^>]*>/s///g
:g/ *>/s//>/g

hope that gives you the idea ...

Thanks
 
You know that you can just put all those commands in a buffer for vi to perform for you. This isn't sed but, I think it will work:
Code:
for fn in *.htm;do
vi $fn <<-EOF
:g/></s//>^M</g
:g/<[^>]*$/j
:g/<\/P[^>]*>/s///g
:g/ *>/s//>/g
:wq
EOF
done
I too would be interested in the sed method, but while we wait - please try this kludge. Einstein47
(&quot;If vegetarians eat vegetables, what do humanitarians eat?&quot;)
 
for CLIediting in ed ex
do
man ${CLIediting}
done; vlad
+---------------------------+
|#include<disclaimer.h> |
+---------------------------+
 
good vlad.
prepend edittool(don't remember the name)
and append vi to the list.
the ';' after 'done' will sure be mis-undestanded
:) vox clamantis in deserto.
 
yeah, cheers for that.

1) can i do an equivalent of join in sed ... and if so, can it do them repeatedly for a given criteria
2) i can do substitutions in sed easy enough thankyou ...

i know i could write this thing in awk if i needed to, but i thought it'd be interesting if one of you guys could show me a sed equivalent.
 
Code:
nawk '
{outstr=$0}
outstr ~ /<[^>]*$/ {
    while (match(outstr,&quot;<[^>]*$&quot;) > 0) {
        getline;
        outstr=sprintf(&quot;%s%s&quot;,outstr,$0);
    }
}
{
    gsub(&quot;>[ ]*<&quot;,sprintf(&quot;>\n<&quot;),outstr);
    gsub(&quot;<\/P[^>]*>&quot;,&quot;&quot;,outstr);
    gsub(&quot; *>&quot;,&quot;>&quot;,outstr);
    print outstr;
}' <file>.html

which works fine until you get a very long concatenation of strings in the while loop ...
i guess i could do with a 2 commands, one to split the >< first then one to join after.

e.g.
Code:
nawk '
{
    gsub(&quot;>[ ]*<&quot;,sprintf(&quot;>\n<&quot;)); 
    print;
}' <file>.html |
nawk '
{
    outstr=$0
}
outstr ~ /<[^>]*$/ {
    while (match(outstr,&quot;<[^>]*$&quot;) > 0) {
        getline;
        outstr=sprintf(&quot;%s%s&quot;,outstr,$0);
    }
}
{
    gsub(&quot;<\/P[^>]*>&quot;,&quot;&quot;,outstr);
    gsub(&quot; *>&quot;,&quot;>&quot;,outstr);
    print outstr;
}'[code]

which does work ...

however, back to my original question, can anyone do this nicely in sed? :)
 
to join lines in vi:
:adr,adrj
in sed:
{
N
s/\n/ /
} vox clamantis in deserto.
 
hmm ... got a sed version to work ... sort of, has same limitation with combining lines though ... they do get a bit long (and i need to do it as 2 passes) ... but i can't get it to be clean and put in \n's easily ...
Code:
sed 's/> *</>\n</' file.html
doesn't work for example.

btw. a cleaned up version of the nawk is available:
Code:
nawk '
{
    gsub(&quot;> *<&quot;,&quot;>\n<&quot;);
    print;
}' <file>.html |
nawk '
{
    outstr=$0
}
outstr ~ /<[^>]*$/ {
    while (match(outstr,&quot;<[^>]*$&quot;) > 0) {
        getline;
        outstr=sprintf(&quot;%s %s&quot;,outstr,$0);
    }
}
{
    gsub(&quot;<\/P[^>]*>&quot;,&quot;&quot;,outstr);
    gsub(&quot; *>&quot;,&quot;>&quot;,outstr);
    print outstr;
}'

the sed code i wrote went like this:
Code:
#!/bin/sh
sed '
:Begin
s/> *</>¬</
/<[^>]*$/ {
N
s/\n/ /
b Begin
}
s/<\/P[^>]*>//
s/ *>/>/
' ch09_01.htm | tr '¬' '\n'

i chose the ¬ character because it shouldn't really ever be in an html file :)
 
Instead of piping to
Code:
tr '¬' '\n'

you could add into your sed script two consecutive lines as:
Code:
s/¬//g

The backslash allows the substitution to use the newline.
Code:
s/¬/\n/g
doesn't work unless you can snatch the newline from the pattern space somewhere (a type of substitution that wouldn't apply here anyway).
Cheers,
ND [smile]

bigoldbulldog@hotmail.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top