Prev: can a DHCP request recieved on eth0 also assign an IP to eth1?
Next: Simple line-printing program
From: Todd on 11 May 2010 13:13 On 05/10/2010 01:33 PM, A Watcher wrote: > I hate awk. The commmands are so cryptic that whomever tries to read the > code later has to work at it to understand it. I hear you there. I keep a list of examples. That is the only way I remember.
From: pk on 26 May 2010 04:43 Dave U. Random wrote: > It can be done with 'sed' and a somewhat creative use of its hold space > for matched patterns' score keeping. It does scale well. It will > definitely be slower than the equivalent grep solution, but for small > data sets it shouldn't matter much. > > The following example finds all positive integers up to 50 that > contain the digits 2 and 4. > > seq 50 | > sed -n ' > # find simultaneous occurrence of many search patterns on the same input = > line > # > # store a N-character string in hold space; > # the replacement string in the following s/// command must be N > # chars long, where N is the number of patterns to match > x;s/.*/12/;x > #################### > # add all your match patterns here > # /pattern1/{x;s/.//;x;} > # /pattern2/{x;s/.//;x;} > # ... > # /patternN/{x;s/.//;x;} > /2/{x;s/.//;x;} > /4/{x;s/.//;x;} > ################### > # ^------------- delete a char from hold space for each matched pattern > { > # make sure that any successful pattern matches above don`t > # overshadow the results of the following s/// test; ie. clear test flag > T > # is the hold space empty, ie. did all patterns match?=20 > x;s/^$//;x > T > p # yes; print input line > } > ' That seems a bit overkill: sed '/pattern1/{h;b end;} sed '/pattern2/{h;b end;} sed '/pattern3/{h;b end;} .... :end ${x;s/\n//p;} d'
From: pk on 26 May 2010 04:47 pk wrote: >> seq 50 | >> sed -n ' >> # find simultaneous occurrence of many search patterns on the same input >> # = >> line >> # >> # store a N-character string in hold space; >> # the replacement string in the following s/// command must be N >> # chars long, where N is the number of patterns to match >> x;s/.*/12/;x >> #################### >> # add all your match patterns here >> # /pattern1/{x;s/.//;x;} >> # /pattern2/{x;s/.//;x;} >> # ... >> # /patternN/{x;s/.//;x;} >> /2/{x;s/.//;x;} >> /4/{x;s/.//;x;} >> ################### >> # ^------------- delete a char from hold space for each matched pattern >> { >> # make sure that any successful pattern matches above don`t >> # overshadow the results of the following s/// test; ie. clear test flag >> T >> # is the hold space empty, ie. did all patterns match?=20 >> x;s/^$//;x >> T >> p # yes; print input line >> } >> ' > > That seems a bit overkill: > > sed '/pattern1/{h;b end;} > sed '/pattern2/{h;b end;} > sed '/pattern3/{h;b end;} > ... > :end > ${x;s/\n//p;} > d' Sorry, I misunderstood what you meant. Your code and mine do different things.
|
Pages: 1 Prev: can a DHCP request recieved on eth0 also assign an IP to eth1? Next: Simple line-printing program |