help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: A very simple question on SED or AWK for a GURU, possibly a lisp scr


From: Wayne Throop
Subject: Re: A very simple question on SED or AWK for a GURU, possibly a lisp script or emacs batch processing of many files
Date: Tue, 14 Jan 2003 22:10:59 GMT

: gnuist006@hotmail.com (gnuist006)
: Even though the problem posed in this thread is still
: 
: *** UNSOLVED ***
: 
: let me extend my gratitude to Mr Christofer and Friedrich for their
: kind attempts to answer this.  I hope for some more help tonight. 

    perl -pe 's-\b(label="[^"]*")- ((($x=$1) =~ s./._.g),$x) -ge'

: I can write a bash wrapper loop but what I do not know here is how to
: implement the core logic of replacing an indefinite number of
: forward-slashes within a pattern of interest. 

    s/\//_/g

: I also want to use bash/sed/awk instead of perl. 

Why?

Oh, well.  You'd think this bit of awk-wardness would work
by analogy with the perl above  `

    awk '{gsub("label=\"[^\"]*\"", gensub("\/","_","g","&"));print}'

but it doesn't.  Hrm.  Maybe

    awk '{
        s=$0;
        m=match(s,"label=\"[^\"]*\"");
        if(m){
            pre =substr(s,1,RSTART-1);
            inf =substr(s,RSTART,RLENGTH);
            post=substr(s,RSTART+RLENGTH);
            gsub("/","_",inf);
            s= pre inf post;
        }
        print s;
    }'

Yeah, that works, at least on the cases I tested.  The perl is a tiny
bit cleaner, though; the perl version handles multiple labels on a line,
and the \b ensures the "label" isn't part of a larger word.  Both of
which are a bit tricky to do in awk.  Doable, just not as easy. 

Plus which, the perl looks more like line noise,
which is cool, and promotes job security.


Wayne Throop   throopw@sheol.org   http://sheol.org/throopw


reply via email to

[Prev in Thread] Current Thread [Next in Thread]