0

My problme is two fold. First is printing a regex match. The second is the actual regex.

Problem 1: I'm trying to print the output of yum grouplist --verbose into a format that I can put into a puppet conf file. Therefore, I need to print more than just the match so sed and grep -o won't work (at least not that I know of).

Here are examples of some of the output:

   Maori Support (maori-support) [mi]
   Mongolian Support (mongolian-support) [mn]
   Mynamar (Burmese) Support (burmese-support) [my]
   Nepali Support (nepali-support) [ne]

I want all output lines that contain (*-support) to be printed out as:

 'maori-support': ensure => absent;
 'mongolian-support': ensure => absent;
 'burmese-support': ensure => absent;
 'nepali-support': ensure => absent;

I've already completed doing it manually, but I'd like to figure out how to do it as a one-line just for knowledge sake.

I've tried awk and perl to no avail. The closest I've been able to get is using perl like this:

 $ yum grouplist --verbose | perl -nwe 'print "$1: ensure\t=> absent;\n" if /\((.+-support)\)'

Problem 2: however, that doesn't include the single quotes into the output. There's also the problem that one of the output lines looks like this:

 Mynamar (Burmese) Support (burmese-support) [my]

The above regex matches everything from the first opening parenthesis. I don't know how to get it to start the match from the second parenthesis. I've tried modifying the regex to be non-greedy with ? but it just continues ot match (Burmese) Support (burmese-support)

2
  • what is the output you wish to see? Jul 17, 2014 at 19:29
  • @glennjackman please my edit
    – chizou
    Jul 17, 2014 at 19:31

2 Answers 2

1
yum grouplist --verbose 2>/dev/null |
sed -n "s/.*(\([^)]\+-support\)).*/'\1': ensure => absent;/p"

Using double quotes for the sed script since you can't embed single quotes in a single quoted string.

With your sample input:

echo "   Maori Support (maori-support) [mi]
   Mongolian Support (mongolian-support) [mn]
   Mynamar (Burmese) Support (burmese-support) [my]
   Nepali Support (nepali-support) [ne]
" | sed -n "s/.*(\([^)]\+-support\)).*/'\1': ensure => absent;/p"
'maori-support': ensure => absent;
'mongolian-support': ensure => absent;
'burmese-support': ensure => absent;
'nepali-support': ensure => absent;
2
  • thanks! that was awesome. i think i understand what your regex is doing, but i'm not 100% certain. did you add the ^) class to throw out matches that include a closing parentheses so that it would start a match from the second group of parentheses?
    – chizou
    Jul 17, 2014 at 20:07
  • 1
    with a string like (a) (b) (c), the basic regex (.*) would mach the whole string. with ([^)]\+), you are matching an open parenthesis and a close parenthesis, and in between you have one or more character that are not a close parentheses. That would match (a) in the string. This is a technique to make your regex less "greedy". Jul 18, 2014 at 2:07
0

Another take, if you like your regexen simple

rev | cut -d ' ' -f2 | rev | tr '()' "'" | sed -n "/-support'/s/\$/: ensure => absent;/p"

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .