🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: gnu.misc.discuss
Article #2157

Re: A very simple question on SED or AWK for a GURU, possibly a lisp script or emacs batch processing of many files

#2157
From: Wayne Throop
Date: Tue, 14 Jan 2003 22:10
50 lines
2858 bytes
: 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
Technical Details
Message-ID: <1042582259@sheol.org>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!cyclone.bc.net!sjc70.webusenet.com!news.webusenet.com!gestalt.direcpc.com!telocity-west!DIRECTV!sn-xit-03!sn-xit-01!sn-post-02!sn-post-01!supernews.com!corp.supernews.com!not-for-mail
References: <b00bb831.0301131603.34e9704c@posting.google.com> <b00bb831.0301140042.4dff4b4e@posting.google.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f4fd2,1e8c4b23af1e4636
X-Google-Attributes: gidf4fd2,public
X-Google-Thread: 103b8b,1e8c4b23af1e4636
X-Google-Attributes: gid103b8b,public
X-Google-Thread: 1025b4,1e8c4b23af1e4636
X-Google-Attributes: gid1025b4,public
X-Google-Thread: 1012a6,6b5030ec1bcea943
X-Google-Attributes: gid1012a6,public
X-Google-ArrivalTime: 2003-01-14 14:39:26 PST
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!cyclone.bc.net!sjc70.webusenet.com!news.webusenet.com!gestalt.direcpc.com!telocity-west!DIRECTV!sn-xit-03!sn-xit-01!sn-post-02!sn-post-01!supernews.com!corp.supernews.com!not-for-mail
From: throopw@sheol.org (Wayne Throop)
Newsgroups: gnu.misc.discuss,comp.lang.lisp,gnu.emacs.help,comp.unix.shell
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
Organization: sheol
Message-ID: <1042582259@sheol.org>
Distribution: world
References: <b00bb831.0301131603.34e9704c@posting.google.com>
    <b00bb831.0301140042.4dff4b4e@posting.google.com>
Cache-Post-Path: sj-nntpcache-5!unknown@tent.cisco.com
X-Cache: nntpcache 2.4.0b2 (see http://www.nntpcache.org/)
X-Complaints-To: abuse@supernews.com
Lines: 50