I am writing bash script to replicate channels from one satellite to another.
I want to get only the list of child channels for the base channel: clone-rhel-x86_64-server.
I have intentionally used grep -E -A10 to give the option output of up to 10 child channels.
Right now I am getting:
rhn-satellite-exporter --list-channels |grep -E -A10 '(^| )clone-rhel-x86_64-server( |$)'|grep -v "^B"
C child_channel1
C child_channel2
C child_channel3
C child_channel4
C child_channel5
C child_channel7
My purpose is go get only the first section, ie only the child channels.
For base channel: clone-rhel-x86_64-server
rhn-satellite-exporter --list-channels |grep -E -A10 '(^| )clone-rhel-x86_64-server( |$)'|grep -v "^B"
C child_channel1
C child_channel2
C child_channel3
How do I achieve that?
One way to do this would be to use perl
's paragraph mode. From man perlrun
:
-0[octal/hexadecimal]
specifies the input record separator ($/) as an octal or
hexadecimal number. [. . .]
The special value 00 will cause Perl to slurp files in paragraph
mode. [. . .]
In paragraph mode, "lines" are defined by \\n\\n
and not \\n
alone, so each "line" is actually a paragraph. You can therefore use a Perl one-liner and tell it to print the first line and exit:
rhn-satellite-exporter --list-channels |
grep -E -A10 '(^| )clone-rhel-x86_64-server( |$)'| grep -v "^B" |
perl -00ne 'print;exit'
Note that the above will also print the empty line since that is considered part of the paragraph. To avoid that, you could either parse it out:
rhn-satellite-exporter --list-channels |
grep -E -A10 '(^| )clone-rhel-x86_64-server( |$)'| grep -v "^B" |
perl -00ne 'print;exit' | grep .
Or remove it in the Perl script itself:
rhn-satellite-exporter --list-channels |
grep -E -A10 '(^| )clone-rhel-x86_64-server( |$)'| grep -v "^B" |
perl -00ne 's/\n\s*\n/\n/;print;exit' | grep .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.