Re: OT: sed/awk pointers needed

2014-10-02 Thread Francois Claire

Hi Craig,



You can pipe the following awk command:

awk '/has the variants/ {curport=$1; next} NF==1  $1~/\[\+\]/ {print 
curport,substr($1,4),Default; next} NF==1 {print curport,$1,N}'



Francois.

Le 30/09/2014 17:44, Craig Treleaven a écrit :

tl;dr version:
How do I transform a listing like this:

$ port variants php53-mysql php54-mysql |grep -v conflicts |grep -e 
mariadb -e mysql -e percona | cut -d : -f 1

php53-mysql has the variants
   mariadb
   mysql4
   mysql5
   mysql51
   mysql55
   mysql56
[+]mysqlnd
   percona
php54-mysql has the variants
   mariadb
   mysql4
   mysql5
   mysql51
   mysql55
   mysql56
[+]mysqlnd
   percona

into:

php53-mysql   mariadb N
php53-mysql   mysql4 N
php53-mysql   mysql5 N
php53-mysql   mysql51 N
php53-mysql   mysql55 N
php53-mysql   mysql56 N
php53-mysql   mysqlnd Default
php53-mysql   percona N
php54-mysql   mariadb N
php54-mysql   mysql4 N
php54-mysql   mysql5 N
php54-mysql   mysql51 N
php54-mysql   mysql55 N
php54-mysql   mysql56 N
php54-mysql   mysqlnd Default
php54-mysql   percona N


I have only rudimentary acquaintance with sed and less with awk. I'd 
like to learn more and this seemed like an opportunity to do so. From 
some reading [1], I think sed's Hold buffer is the way to extract the 
port name and insert it onto the line with each of the variants.  But 
the tutorial only uses the hold buffer for full lines; not just, say, 
the first word of the line.  Is this possible?  How does one do that?


[1] http://www.grymoire.com/Unix/Sed.html#uh-52

Or maybe I'm barking up the wrong tree?  Is there another 
tool/technique that is better suited?


Thanks in advance,

Craig
___
macports-dev mailing list
macports-dev@lists.macosforge.org
https://lists.macosforge.org/mailman/listinfo/macports-dev



___
macports-dev mailing list
macports-dev@lists.macosforge.org
https://lists.macosforge.org/mailman/listinfo/macports-dev


OT: sed/awk pointers needed

2014-09-30 Thread Craig Treleaven

tl;dr version:
How do I transform a listing like this:

$ port variants php53-mysql php54-mysql |grep -v conflicts |grep -e 
mariadb -e mysql -e percona | cut -d : -f 1

php53-mysql has the variants
   mariadb
   mysql4
   mysql5
   mysql51
   mysql55
   mysql56
[+]mysqlnd
   percona
php54-mysql has the variants
   mariadb
   mysql4
   mysql5
   mysql51
   mysql55
   mysql56
[+]mysqlnd
   percona

into:

php53-mysql   mariadb N
php53-mysql   mysql4 N
php53-mysql   mysql5 N
php53-mysql   mysql51 N
php53-mysql   mysql55 N
php53-mysql   mysql56 N
php53-mysql   mysqlnd Default
php53-mysql   percona N
php54-mysql   mariadb N
php54-mysql   mysql4 N
php54-mysql   mysql5 N
php54-mysql   mysql51 N
php54-mysql   mysql55 N
php54-mysql   mysql56 N
php54-mysql   mysqlnd Default
php54-mysql   percona N


I have only rudimentary acquaintance with sed and less with awk.  I'd 
like to learn more and this seemed like an opportunity to do so. 
From some reading [1], I think sed's Hold buffer is the way to 
extract the port name and insert it onto the line with each of the 
variants.  But the tutorial only uses the hold buffer for full lines; 
not just, say, the first word of the line.  Is this possible?  How 
does one do that?


[1] http://www.grymoire.com/Unix/Sed.html#uh-52

Or maybe I'm barking up the wrong tree?  Is there another 
tool/technique that is better suited?


Thanks in advance,

Craig
___
macports-dev mailing list
macports-dev@lists.macosforge.org
https://lists.macosforge.org/mailman/listinfo/macports-dev


Re: OT: sed/awk pointers needed

2014-09-30 Thread Brandon Allbery
On Tue, Sep 30, 2014 at 11:44 AM, Craig Treleaven ctrelea...@macports.org
wrote:

 I have only rudimentary acquaintance with sed and less with awk.  I'd like
 to learn more and this seemed like an opportunity to do so. From some
 reading [1], I think sed's Hold buffer is the way to extract the port name
 and insert it onto the line with each of the variants.  But the tutorial
 only uses the hold buffer for full lines; not just, say, the first word of
 the line.  Is this possible?  How does one do that?


Very, very painfully. I think I could do it, with a LOT of fiddling, but
would very much prefer to use a more appropriate tool. awk is somewhat
better, but I'd reach for perl/python/ruby first.

Very roughly, the trick is to chop the line down to the first word, save it
to the hold buffer, then on subsequent lines append hold space to pattern
space, swap the line around the embedded newline that gets inserted before
the appendage, then replace that newline with a space. Use branch testing
to detect the difference between the two kinds of lines:

s/^  //
t dataline
s/ .*$//
h
b
: dataline
G
s/^\(.*\)\n\(.*\)$/\2 \1/
# now starts with the package name and a space; put the rest of the
processing here...

You can see how painful it gets. (Also the above is untested.) Better to
use a real language.

-- 
brandon s allbery kf8nh   sine nomine associates
allber...@gmail.com  ballb...@sinenomine.net
unix, openafs, kerberos, infrastructure, xmonadhttp://sinenomine.net
___
macports-dev mailing list
macports-dev@lists.macosforge.org
https://lists.macosforge.org/mailman/listinfo/macports-dev


Re: OT: sed/awk pointers needed

2014-09-30 Thread Craig Treleaven

At 2:27 PM -0400 9/30/14, Lawrence Velázquez wrote:

On Sep 30, 2014, at 12:00 PM, Brandon Allbery allber...@gmail.com wrote:

 On Tue, Sep 30, 2014 at 11:44 AM, Craig 
Treleaven ctrelea...@macports.org wrote:
 I have only rudimentary acquaintance with sed 
and less with awk.  I'd like to learn more and 
this seemed like an opportunity to do so. From 
some reading [1], I think sed's Hold buffer is 
the way to extract the port name and insert it 
onto the line with each of the variants.  But 
the tutorial only uses the hold buffer for 
full lines; not just, say, the first word of 
the line.  Is this possible?  How does one do 
that?


 Very, very painfully. I think I could do it, 
with a LOT of fiddling, but would very much 
prefer to use a more appropriate tool. awk is 
somewhat better, but I'd reach for 
perl/python/ruby first.


Yup. Sed is not a great tool for this sort of 
thing; the functionality it provides is 
ill-suited for complex conditionals and 
branching. You'll regret writing the script as 
soon as you go to read/edit it in a week or 
month.


vq

--8--

That being said, I have masochistic tendencies.

% port variants php53-mysql php54-mysql | sed -nf variants.sed
php53-mysql mariadb N
php53-mysql mysql4  N
php53-mysql mysql5  N
php53-mysql mysql51 N
php53-mysql mysql55 N
php53-mysql mysql56 N
php53-mysql mysqlnd Default
php53-mysql percona N
php54-mysql mariadb N
php54-mysql mysql4  N
php54-mysql mysql5  N
php54-mysql mysql51 N
php54-mysql mysql55 N
php54-mysql mysql56 N
php54-mysql mysqlnd Default
php54-mysql percona N
%


Attachment converted: Macintosh HD:variants.sed (/) (0342ACEF)


Thank you!  Off list, I was pointed to a sample 
Awk program* that reformats a 'billing report'. 
That helped; you've given me a complete solution!


* http://www.programmingforums.org/post226750.html

APL used to have the reputation as a write-only 
language.  This script is pretty close to that 
ideal.  ;)


Craig
___
macports-dev mailing list
macports-dev@lists.macosforge.org
https://lists.macosforge.org/mailman/listinfo/macports-dev


Re: OT: sed/awk pointers needed

2014-09-30 Thread Lawrence Velázquez
On Sep 30, 2014, at 4:02 PM, Craig Treleaven ctrelea...@macports.org wrote:

 you've given me a complete solution!

I just realized that you're probably using N to signify that a variant is not 
selected, so Y should be used if a variant *is* selected.


# Save port to hold space.
/ has the variants/ {
s/ has .*$//
h
d
}

# Remove noise.
/conflicts with/d
/^[^:]*mariadb[^:]*:/b db_variant
/^[^:]*mysql[^:]*:/b db_variant
/^[^:]*percona[^:]*:/b db_variant
d

:db_variant

# Append desired suffix to variant name.
/^\[+\]/s/.*]\(.*\):.*$/\1  Default/
/^[[:blank:]]*+/s/[[:blank:]]*+\(.*\):.*$/\1Y/
/^[[:blank:]]*[^+]/s/[[:blank:]]*\(.*\):.*$/\1  N/

# Extract port name from hold space and append variant name and suffix.
x
s/[[:blank:]].*$//
G
s/\n/   /

# Print pattern space and save to hold space for subsequent lines.
p
h


vq
___
macports-dev mailing list
macports-dev@lists.macosforge.org
https://lists.macosforge.org/mailman/listinfo/macports-dev


Re: OT: sed/awk pointers needed

2014-09-30 Thread Craig Treleaven

At 2:27 PM -0400 9/30/14, Lawrence Velázquez wrote:

On Sep 30, 2014, at 12:00 PM, Brandon Allbery allber...@gmail.com wrote:
  On Tue, Sep 30, 2014 at 11:44 AM, Craig 
Treleaven ctrelea...@macports.org wrote:
  I have only rudimentary acquaintance with 
sed and less with awk.  I'd like to learn more 
and this seemed like an opportunity to do so. 
[...]

 
  Very, very painfully. I think I could do it, 
with a LOT of fiddling, but would very much 
prefer to use a more appropriate tool. awk is 
somewhat better, but I'd reach for 
perl/python/ruby first.


[vq's sed version deleted ...]


As I said, it was a learning exercise and I've 
got a working version with gawk.  (Awk lacks the 
sub()/gsub() string functions.)


My version is:

-cut from here---
# identify port name lines by the phrase  has the variants
/ has the variants/ {
 portname = $1
 next
}

# deal with the default variant lines, they start with [+]
# (which is a crazy string to match with a regex)
# note that the regex in the pattern match and the sub() function are
# (must be) wildly different!
/^\[+./ {
  sub(\\[\\+.,)
  print portname ,   $1 ,  Default
  next
}

# All the other variant lines start with 3 spaces, followed by a lower case
# variant name
/^   [a-z]/ {
  print portname ,   $1 ,  N
  next
}
-to here

And it works as follows:
$ port variants php53-mysql php54-mysql |grep -v 
conflicts |grep -e mariadb -e mysql -e percona | 
cut -d : -f 1 | gawk -f rejig.awk -

php53-mysql, mariadb, N
php53-mysql, mysql4, N
php53-mysql, mysql5, N
php53-mysql, mysql51, N
php53-mysql, mysql55, N
php53-mysql, mysql56, N
php53-mysql, mysqlnd, Default
php53-mysql, percona, N
php54-mysql, mariadb, N
php54-mysql, mysql4, N
php54-mysql, mysql5, N
php54-mysql, mysql51, N
php54-mysql, mysql55, N
php54-mysql, mysql56, N
php54-mysql, mysqlnd, Default
php54-mysql, percona, N

Now to see if I can actually run this against the 
89 ports that appear to have mysql-related 
variants...


Craig
___
macports-dev mailing list
macports-dev@lists.macosforge.org
https://lists.macosforge.org/mailman/listinfo/macports-dev