変更された文字列を使用して段落をコピーする方法

変更された文字列を使用して段落をコピーする方法

段落をコピーし、ユーザーを変更し、同じファイルに挿入できるものを探しています。

前のファイル:

user1
  this is only
  a test of 
  a lovely idea

user2
  this user shhould
  be copied

user3
  who has an
  idea for
  my problem

それ以降のファイル(user2次に検索、コピー、挿入user4):

user1
  this is only
  a test of 
  a lovely idea

user2
  this user shhould
  be copied

user3
  who has an
  idea for
  my problem

user4
  this user shhould
  be copied

ベストアンサー1

#!/bin/perl
#
use strict;

local $/="\n\n";

my $match = shift @ARGV;
my $subst = shift @ARGV;
my $save;

while (defined (my $paragraph = <>))
{
    $paragraph =~ s/\n+$//;
    $paragraph .= "\n";

    my $user = ($paragraph =~ m/(\w+)\n/)[0];
    if ($match eq $user)
    {
        $save = $paragraph;
        $save =~ s/\b$user\b/$subst/g
    };

    print "$paragraph\n"
}
print "$save" if defined $save;
exit 0

このようにしてください

script.pl user2 user4 <file

おすすめ記事