Using Raku (formerly known as Perl_6)
~$ raku -e 'my $i=0; my @a; for lines() { \ $i=1 and next if /^ \s+ SERVICE_NAME /; \ if ($i.Bool) { @a.push: .words.skip.join("_") } else {next};}; \ .put for @a.join(",");' file
OR
~$ raku -e 'my @a; for lines() { \ @a.push( $_.words.skip.join: "_" ) if /^ \s+ SERVICE_NAME / ^fff * }; \ .put for @a.join(",");' file
Sample Input:
Controller loading lists... ------------------------------------------------- command: select SERVICE_NAME from <table_name> ------------------------------------------------- SERVICE_NAME 1 first service 2 second service
Sample Output:
first_service,second_service
Raku is a programming language in the Perl-family. The project was started in year 2000 by Larry Wall and colleagues. The first release was in December 2015. The Perl6 language was renamed Raku in 2019.
First answer above: the code declares a scalar $i
and an array @a
. Input is taken with lines()
. Inside the {...}
block, a sentinel line ^ \s+ SERVICE_NAME
is searched for. If found, $i
is set to 1
and the code proceeds to the next
line.
From here, the last two statements run if $i
coerced to a Bool
ean is True
. When $i.Bool
is True
, the {...}
block is entered: the line is broken into whitespace separated .words
, the first word (i.e. the number) is skip
ped and the remainder join
ed with underscores. Finally, the line is output
, join
ed with commas.
Second answer above: The code uses Raku's sed
-like "flip-flop" operator, fff
and variants. The ^fff
variant in the code turns ON for capturing lines at the line after the SERVICE_NAME
sentinel line is found. The *
means all lines until the end of the target file are push
ed onto the array. See discussion above for text-processing and output
.
https://docs.raku.org/language/operators#infix_^fff
https://raku.org
Controller loading lists...
or similar)?GROUP_CONCAT()
or similar functionality), not by postprocessing the output. Unfortunately, you never say what SQL database backend you're using.