Eu gostaria de usar uma linha como prefixo para mais algumas linhas. Por exemplo, considere a situação em que tenho estas linhas:
hostname-junek-1
;ge-1/0/1
;ge-1/0/2
;ge-1/0/5
;ge-1/1/27
;ge-1/1/39
hostname-junek-2
;ge-1/0/5
;ge-2/0/27
;ge-2/0/39
hostname-junek-3
;ge-1/1/1
;ge-2/1/13
;ge-2/1/17
;ge-2/1/11
Eu gostaria de receber algo assim:
hostname-junek-1;ge-1/0/1
hostname-junek-1;ge-1/0/2
hostname-junek-1;ge-1/0/5
hostname-junek-1;ge-1/1/27
hostname-junek-1;ge-1/1/39
hostname-junek-2;ge-1/0/5
hostname-junek-2;ge-2/0/27
hostname-junek-2;ge-2/0/39
hostname-junek-3;ge-1/1/1
hostname-junek-3;ge-2/1/13
hostname-junek-3;ge-2/1/17
hostname-junek-3;ge-2/1/11
Como eu faço isso? Usando o notepad ++ ou outra coisa.
Responder1
Receio que isso não possa ser feito com o Notepad++.
Aqui está uma solução com um oneliner perl:
cat inputfile
hostname-junek-1
;ge-1/0/1
;ge-1/0/2
;ge-1/0/5
;ge-1/1/27
;ge-1/1/39
hostname-junek-2
;ge-1/0/5
;ge-2/0/27
;ge-2/0/39
hostname-junek-3
;ge-1/1/1
;ge-2/1/13
;ge-2/1/17
;ge-2/1/11
perl -ane 'if(/^(hostname-junek-\d+)\R/) {$host=$1;next}print "$host$_"' inputfile > outputfile
cat outputfile
hostname-junek-1;ge-1/0/1
hostname-junek-1;ge-1/0/2
hostname-junek-1;ge-1/0/5
hostname-junek-1;ge-1/1/27
hostname-junek-1;ge-1/1/39
hostname-junek-2;ge-1/0/5
hostname-junek-2;ge-2/0/27
hostname-junek-2;ge-2/0/39
hostname-junek-3;ge-1/1/1
hostname-junek-3;ge-2/1/13
hostname-junek-3;ge-2/1/17
hostname-junek-3;ge-2/1/11
Explicação:
/ # regex delimiter
^ # beginning of line
(hostname-junek-\d+) # group 1, hostname...followed by 1 or more digits
\R # any kind of linebreak
/ # regex delimiter
if the regex matches the current line, {
$host=$1; # save the hostname
next # skip to next line
} # endif
print "$host$_" # print the content of $host followed by the current line