如何按顺序编辑多个文件的相同两行?

发布于 2025-01-19 04:06:59 字数 1025 浏览 5 评论 0原文

我有一组元数据,需要

  • 在每个字段上编辑带有序列号第 2 行的字段: <前><代码>“名称”:“#1”
  • 每行第 5 行: <前><代码>“版本”:1,

我需要用 580 个文件中的连续数字替换每行上的 1。

这就是我想要完成的任务:

1.json

{
  "name": "#1",
  "description": "description",
  "image": "imagelink",
  "edition": 1,

2.json

{
  "name": "#2",
  "description": "description",
  "image": "imagelink",
  "edition": 2,

3.json

{
  "name": "#3",
  "description": "description",
  "image": "imagelink",
  "edition": 3,

我正在尝试修改此处的代码以适合我的场景:

https://unix.stackexchange.com/ questions/508423/add-a-sequential-number-in-a-pspecial-line-for-multiple-files

我尝试过

perl -pi -e '$_ = sprintf("name": %01d %s", ++$n, $_) if $. == 1; close ARGV if eof' *.json 

当我尝试这个时,我没有在任何文件中得到任何更改。

I have a set of metadata where I need to edit the fields with a sequential number

  • Line 2 on each:
    "name": "#1"
    
  • Line 5 on each:
    "edition": 1,
    

I need to replace 1 on each line with sequential numbers in 580 files.

This is what I am trying to accomplish:

1.json

{
  "name": "#1",
  "description": "description",
  "image": "imagelink",
  "edition": 1,

2.json

{
  "name": "#2",
  "description": "description",
  "image": "imagelink",
  "edition": 2,

3.json

{
  "name": "#3",
  "description": "description",
  "image": "imagelink",
  "edition": 3,

etc

I am trying to modify the code here to work for my scenario:

https://unix.stackexchange.com/questions/508423/add-a-sequential-number-in-a-particular-line-for-multiple-files

I tried

perl -pi -e '$_ = sprintf("name": %01d %s", ++$n, $_) if $. == 1; close ARGV if eof' *.json 

When I try this I dont get any change in any of the files.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

没有伤那来痛 2025-01-26 04:06:59

在处理格式时,使用真正理解 JSON 的方法总是更容易、更可靠。以下使用了几个非核心模块, Path::TinyJSON::MaybeXS (如果您使用 Homebrew 进行 Mac 包管理,我看不到它们可用的但您可以安装 cpanminus 然后执行 cpanm Path::Tiny JSON::MaybeXS 安装它们。)

#!/usr/bin/env perl                                                                                                                                                                                                                               
use warnings;
use strict;
use feature qw/say/;
use Path::Tiny;
use JSON::MaybeXS;

my $json = JSON::MaybeXS->new->utf8->pretty;
# For each file matching the RE in the current directory
foreach my $file (path(".")->children(qr/^\d+\.json$/)) {
  say "File $file";
  # Calculate the number based on the filename and update the JSON
  # object in-place
  $file->edit_raw(sub {
                    my $num = $file->basename(".json");
                    my $obj = $json->decode($_);
                    $obj->{name} = "#$num";
                    $obj->{edition} = $num + 0;
                    $_ = $json->encode($obj);
                  });
}

It's always easier and more robust to use an approach that actually understands JSON when working with the format. The following uses a couple of non-core modules, Path::Tiny and JSON::MaybeXS (If you're using homebrew for Mac package management, I don't see them available but you can install cpanminus and then do a cpanm Path::Tiny JSON::MaybeXS to install them.)

#!/usr/bin/env perl                                                                                                                                                                                                                               
use warnings;
use strict;
use feature qw/say/;
use Path::Tiny;
use JSON::MaybeXS;

my $json = JSON::MaybeXS->new->utf8->pretty;
# For each file matching the RE in the current directory
foreach my $file (path(".")->children(qr/^\d+\.json$/)) {
  say "File $file";
  # Calculate the number based on the filename and update the JSON
  # object in-place
  $file->edit_raw(sub {
                    my $num = $file->basename(".json");
                    my $obj = $json->decode($_);
                    $obj->{name} = "#$num";
                    $obj->{edition} = $num + 0;
                    $_ = $json->encode($obj);
                  });
}
最美不过初阳 2025-01-26 04:06:59

以下代码片段解决了我的问题:

perl -pi -e 's/#1/sprintf("#". ++$n)/e' *.json(n)

第一个解决了将 #1 替换为 #(序号) 的问题

perl -pi -e 's/: 1/sprintf(": ".++$n)/e' *.json(n)

第二个解决了将 : 1 (在第 5 行中找到) 替换为 :(序号) 的问题

The following code snippets solved my problem:

perl -pi -e 's/#1/sprintf("#". ++$n)/e' *.json(n)

The first addressed the issue of replacing #1 with #(sequential numbers)

perl -pi -e 's/: 1/sprintf(": ".++$n)/e' *.json(n)

The second addresses the issue of replacing : 1 (found in line 5) with :(sequential numbers)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文