从一行中提取一个字符串并将其放在前一行 (Perl)

Extract a string from one line and put it in the precceding line (Perl)

我没有太多使用 perl 编程的经验,但必须解决一个繁重的问题 problem.I 有这种格式的数据:

*IDENTIFIER A  
ABCDEFGHIJKLMNOPQRSTVUWXYZ  
3. line  
4. line  
*IDENTIFIER B  
ABCDEFGHIJKLMNOPQRSTVUWXYZ  
3. line  
4. line  
...  

我想从标识符下的行中删除前 5 个符号,并将它们添加到标识符行中。每个标识符都以 * 开头。新文件应如下所示:

*IDENTIFIER A:ABCDE  
FGHIJKLMNOPQRSTVUWXYZ  
3. line  
4. line  
*IDENTIFIER B:ABCDE  
FGHIJKLMNOPQRSTVUWXYZ  
3. line  
4. line  
...    

如果有任何帮助,我都会很高兴。谢谢

没那么难。

while (<>) {
    if (/^\*/) {                                     # Identifier
        chomp;                                       # Remove the \n.
        my $nextline = <>;                           # Read the next line.
        my $first_5 = substr $nextline, 0, 5, q();   # Move the 1st 5 characters to a variable.
        print "$_:$first_5\n$nextline";              # Print the identifier, the 5 chars,
                                                     #     newline, nextline.
    } else {
        print
    }
}