Uma abordagem Perl:
$ perl -lne 'if(/^(Folder|Workflow|Workflow.*?status|Sched.*time|Integration Service):.*?\[([^][]+)/){++$k%5==0 ? print "$2" : printf "%s,",$2}' file
ALS_DIM, wf_ld_als_dim, Scheduled, Wed Dec 30 19:00:00 2015, TEST_Integration_Service
ALS_FACT, wf_s_m_ld_interchanges_detail_log, Scheduled, Mon Jan 04 16:30:00 2016, TEST_Integration_Service
ALS_PRD, wf_maint_service_fields, Scheduled, Thu Dec 31 07:10:00 2015, TEST_Integration_Service
Ou menos condensado:
$ perl -lne '
if(/^ ## Match the beginning of the line
( ## 1st capturing group: $1
Folder | ## The various things we want to match
Workflow |
Workflow.*?status |
Sched.*time |
Integration\s*Service
): ## Only if they are followed by a :
.*?\[
( ## 2nd caprturing group: $2.
[^][]+ ## The longest string of non-] or [
)/x ## The x allows writing multiline regexes
)
{ ## If this line matches...
$k=$k+1; ## Increment the counter $k by one
if($k%5==0){ ## If the current value of $k is a multiple of 5.
print "$2" ## Print the 2nd captured group and a newline.
} ## The newline is automatically added by the -l.
else{
printf "%s,",$2 ## For other lines, just print with no newline.
}
}' file
ALS_DIM, wf_ld_als_dim, Scheduled, Wed Dec 30 19:00:00 2015, TEST_Integration_Service
ALS_FACT, wf_s_m_ld_interchanges_detail_log, Scheduled, Mon Jan 04 16:30:00 2016, TEST_Integration_Service
ALS_PRD, wf_maint_service_fields, Scheduled, Thu Dec 31 07:10:00 2015, TEST_Integration_Service
Para adicionar o Insert ...
, basta passar por um simples sed
:
$ perl -lne 'if(/^(Folder|Workflow|Workflow.*?status|Sched.*time|Integration Service):.*?\[([^][]+)/){++$k%5==0 ? print "$2" : printf "%s,",$2}' file |
sed "s/^/Insert into <tablename> values('/; s/,/','/g; s/$/')/"
Insert into <tablename> values("ALS_DIM","wf_ld_als_dim","Scheduled","Wed Dec 30 19:00:00 2015","TEST_Integration_Service")
Insert into <tablename> values("ALS_FACT","wf_s_m_ld_interchanges_detail_log","Scheduled","Mon Jan 04 16:30:00 2016","TEST_Integration_Service")
Insert into <tablename> values("ALS_PRD","wf_maint_service_fields","Scheduled","Thu Dec 31 07:10:00 2015","TEST_Integration_Service")
O sed
executa três operadores de substituição:
-
s/^/Insert into <tablename> values("/
:^
é o começo da linha. Portanto,s/^/foo/
simplesmente inserefoo
no início da linha. Aqui, está inserindonsert into <tablename> values("
. -
s/,/','/g
: substitua todas as vírgulas (s///g
) por','
. -
s/$/")/'
:$
é o final da linha, então isso adicionará)"
no final.