2

好在它终于发生了。我的 Google-fu 让我失望了。请帮忙...

我有一个批处理文件,它通过一个目录并从漫画档案(.cbz 文件)中获取信息

它会生成一个包含标题、页数、最后一页分辨率、存档大小和艺术家姓名的 CSV 文件

除了分辨率,这一切都很好。我能够得到解决没有问题,但提取最后一页只有在档案中以特定方式命名文件时才有效(文件被命名为第 000 页,我计算文件数并减去 1)。如果它偏离(第一页是第 801 页,最后一页是第 868 页)它无法提取页面,因为我告诉它提取第 068 页而不是 868。

所以我想如果我只是得到最后一页的实际名称,我就是金。

我正在尝试使用以下方法 grep zip 文件中的最后一个文件名:

7z l filename | grep -o -P Page\s[0-9]{3}\..*(?!Page\s[0-9]{3}\..*)

但这给了我所有的文件名。

这是我试图 grep 的输出:

7-Zip [64] 9.38 beta  Copyright (c) 1999-2014 Igor Pavlov  2015-01-03

Listing archive: Christian Knockers {Pages 0801-0868} [Dark Lord].cbz

--
Path = Christian Knockers {Pages 0801-0868} [Dark Lord].cbz
Type = zip
Physical Size = 224551692

   Date      Time    Attr         Size   Compressed  Name
------------------- ----- ------------ ------------  ------------------------
2020-11-19 15:51:25 ....A      3589432      3589432  Page 801.png
2020-11-19 16:09:29 ....A      3455981      3455981  Page 802.png
2020-11-26 14:48:47 ....A      3017353      3017353  Page 803.png
2020-11-26 15:02:27 ....A      3627637      3627637  Page 804.png
2020-11-26 15:13:05 ....A      3212321      3212321  Page 805.png
<snip>
2021-03-19 15:37:49 ....A      3106721      3106721  Page 864.png
2021-03-19 15:37:19 ....A      2619460      2619460  Page 865.png
2021-03-19 15:37:21 ....A      3063014      3063014  Page 866.png
2021-03-19 15:36:38 ....A      2423233      2423233  Page 867.png
2021-03-19 15:36:41 ....A      2908774      2908774  Page 868.png
------------------- ----- ------------ ------------  ------------------------
2021-03-19 15:38:54          224542422    224542422  68 files

Kernel  Time =     0.015 =   18%
User    Time =     0.000 =    0%
Process Time =     0.015 =   18%    Virtual  Memory =      3 MB
Global  Time =     0.084 =  100%    Physical Memory =      7 MB

我在正则表达式方面越来越好,但只有我使用过的组是捕获组。我用谷歌搜索的内容一直在说消极的前瞻,但我没有任何运气。

任何帮助表示赞赏!

4

1 回答 1

0

利用

grep -zoP '(?s)Page\s[0-9]{3}\.\w+(?!.*Page\s[0-9]{3}\.\w+)' file

-z会将文件视为单行。(?s)将允许点匹配线边界。

解释

--------------------------------------------------------------------------------
  Page                     'Page'
--------------------------------------------------------------------------------
  \s                       whitespace (\n, \r, \t, \f, and " ")
--------------------------------------------------------------------------------
  [0-9]{3}                 any character of: '0' to '9' (3 times)
--------------------------------------------------------------------------------
  \.                       '.'
--------------------------------------------------------------------------------
  \w+                      word characters (a-z, A-Z, 0-9, _) (1 or
                           more times (matching the most amount
                           possible))
--------------------------------------------------------------------------------
  (?!                      look ahead to see if there is not:
--------------------------------------------------------------------------------
    .*                       any character (0 or more times matching the most amount possible)
--------------------------------------------------------------------------------
    Page                     'Page'
--------------------------------------------------------------------------------
    \s                       whitespace (\n, \r, \t, \f, and " ")
--------------------------------------------------------------------------------
    [0-9]{3}                 any character of: '0' to '9' (3 times)
--------------------------------------------------------------------------------
    \.                       '.'
--------------------------------------------------------------------------------
    \w+                      word characters (a-z, A-Z, 0-9, _) (1 or
                             more times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
  )                        end of look-ahead
于 2021-03-19T23:52:16.763 回答