Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我可以用Linux中所有文件中的新内容更新文件部分吗_Linux_File_Shell_Sed_Awk - Fatal编程技术网

我可以用Linux中所有文件中的新内容更新文件部分吗

我可以用Linux中所有文件中的新内容更新文件部分吗,linux,file,shell,sed,awk,Linux,File,Shell,Sed,Awk,我有50个具有此功能的文件 public function __construct() { $this->createdAt = new DateTime(); $this->updatedAt = new DateTime(); $this->isActive = true; $this->isDeleted = false; } 现在有没有任何方法可以使用sed或awk或任何可能的方式将这些行附加到

我有50个具有此功能的文件

public function __construct()
    {   $this->createdAt = new DateTime();
        $this->updatedAt = new DateTime();
        $this->isActive = true;
        $this->isDeleted = false;
    }
现在有没有任何方法可以使用sed或awk或任何可能的方式将这些行附加到函数中的所有文件中

假设file.php是

public function __construct()
    {
        $this->age = 20;

    }
我希望它变成这样

public function __construct()
    {
        $this->age = 20;
        $this->createdAt = new DateTime();
        $this->updatedAt = new DateTime();
        $this->isActive = true;
        $this->isDeleted = false;

    }
这可能适用于您(GNU-sed):

cat$this->createdAt=new DateTime();
>$this->updatedAt=new DateTime();
>$this->isActive=true;
>$this->isDeleted=false;
> !
cat公共函数u_构造()
>     {
>$this->age=20;
> 
>     }
> !
sed“/$this->age=20/r append.txt”文件
公共函数构造()
{
$this->age=20;
$this->createdAt=new DateTime();
$this->updatedAt=new DateTime();
$this->isActive=true;
$this->isDeleted=false;
}
sed-i'/$this->age=20/r append.txt'文件{1..50}#file1到file50
编辑:

要在关闭大括号之前插入,请执行以下操作:

sed -i '$!s/$/\\/' append.txt
sed -i '/^public function __construct()/,/^\s*}/!b;/^\s*}/i\'"$(<append.txt)" file{1..50}
sed-i'$!s/$/\\/'append.txt

sed-i'/^public function\uu construct()/,/^\s*}/!b/^\s*}/i\'”$(是的,你可以。你试过什么?你读过
sed
awk
上的文档吗?我读过sed上的文档,但我不知道如何检查多行是否匹配。我可以做简单的字符串替换,但这对我来说很困难。你可能不需要匹配多行,只需要插入几行。实际上ge=20只是一个例子。它可能有diff行,但事情是在构造函数的终止“}”之前追加行
cat <<\! >append.txt
>         $this->createdAt = new DateTime();
>         $this->updatedAt = new DateTime();
>         $this->isActive = true;
>         $this->isDeleted = false;
> !
cat <<\! >file
> public function __construct()
>     {
>         $this->age = 20;
> 
>     }
> !
sed '/$this->age = 20/r append.txt' file
public function __construct()
    {
        $this->age = 20;
        $this->createdAt = new DateTime();
        $this->updatedAt = new DateTime();
        $this->isActive = true;
        $this->isDeleted = false;

    }
sed -i '/$this->age = 20/r append.txt' file{1..50} # file1 to file50
sed -i '$!s/$/\\/' append.txt
sed -i '/^public function __construct()/,/^\s*}/!b;/^\s*}/i\'"$(<append.txt)" file{1..50}