AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / unix / 问题 / 777976
Accepted
baiserekku
baiserekku
Asked: 2024-06-08 23:35:31 +0800 CST2024-06-08 23:35:31 +0800 CST 2024-06-08 23:35:31 +0800 CST

Bash:对给定模式之前 N 行中出现的数字求和

  • 772

我有一个 kdenlive 项目文件(基本上是一个 xml 文件),里面有很多文本剪辑。我需要批量编辑多次出现的特定文本的 x 坐标。这是一个例子:

  <position x="1316" y="553">
   <transform>1,0,0,0,1,0,0,0,1</transform>
  </position>
  <content line-spacing="0" shadow="0;#64000000;3;3;3" font-underline="0" box-height="94" font="Cambria" letter-spacing="0" font-pixel-size="80" font-italic="0" typewriter="0;2;1;0;0" alignment="4" font-weight="50" box-width="91" font-color="0,255,255,255">MIA!</content>
 </item>
 <item type="QGraphicsTextItem" z-index="7">
  <position x="1106" y="552">
   <transform>1,0,0,0,1,0,0,0,1</transform>
  </position>
  <content line-spacing="0" shadow="0;#64000000;3;3;3" font-underline="0" box-height="94" font="Cambria" letter-spacing="0" font-pixel-size="80" font-italic="0" typewriter="0;2;1;0;0" alignment="4" font-weight="50" box-width="91" font-color="255,255,255,255">6.5</content>
 </item>

我只需要编辑有关文本MIA!的 x 坐标! ,即1316。我想要做的是添加 40 并使 x 坐标变为1356。

文件中有多个MIA!文本,它们的 x 坐标不同。我需要一个脚本,给定一个数字(例如 40),可以将其与 x 坐标相加,并在每次出现 MIA !文本时用相加后的值替换该值。

我的想法是编写一个脚本,将MIA!模式前 3 行找到的 x 坐标保存在一个变量中,然后循环将每个数字相加 40。但我不知道如何替换文件中的新值。

#!/bin/bash
xcoord=`perl -ne 'push @lines,$_; print $lines[0] if /MIA!/; shift(@lines) if $.>3;' "$@"  | cut -d'"' -f2`
for word in $xcoord
do
final=$(( $word + 40 ))
....
done

最终的输出应该是:

  <position x="1356" y="553">
   <transform>1,0,0,0,1,0,0,0,1</transform>
  </position>
  <content line-spacing="0" shadow="0;#64000000;3;3;3" font-underline="0" box-height="94" font="Cambria" letter-spacing="0" font-pixel-size="80" font-italic="0" typewriter="0;2;1;0;0" alignment="4" font-weight="50" box-width="91" font-color="0,255,255,255">MIA!</content>
 </item>
 <item type="QGraphicsTextItem" z-index="7">
  <position x="1106" y="552">
   <transform>1,0,0,0,1,0,0,0,1</transform>
  </position>
  <content line-spacing="0" shadow="0;#64000000;3;3;3" font-underline="0" box-height="94" font="Cambria" letter-spacing="0" font-pixel-size="80" font-italic="0" typewriter="0;2;1;0;0" alignment="4" font-weight="50" box-width="91" font-color="255,255,255,255">6.5</content>
 </item>
bash
  • 3 3 个回答
  • 57 Views

3 个回答

  • Voted
  1. Best Answer
    Stéphane Chazelas
    2024-06-09T01:15:56+08:002024-06-09T01:15:56+08:00

    可能是这样的:

    perl -0777 -pe '
      s{<position x="\K\d+(?=.*?>([^>]*?)</content>)}{
        $& + ($1 eq "MIA!")*40
      }gse' your-file
    
    • 1
  2. Ed Morton
    2024-06-09T18:00:35+08:002024-06-09T18:00:35+08:00

    在每个 Unix 机器上的任意 shell 中使用任意 awk,无需将整个输入读入内存,也无需读取输入两次:

    $ cat tst.sh
    #!/usr/bin/env bash
    
    awk -v delta=40 -F'"' '
        /^ *<position/ {
            xval = $2
        }
        xval == "" {
            print
            next
        }
        {
            buf = buf $0 ORS
            if ( /^ *<content/ ) {
                if ( /MIA!/ ) {
                    sub(xval,xval + delta,buf)
                }
                printf "%s", buf
                xval = buf = ""
            }
        }
    ' "$1"
    

    $ ./tst.sh file
      <position x="1356" y="553">
       <transform>1,0,0,0,1,0,0,0,1</transform>
      </position>
      <content line-spacing="0" shadow="0;#64000000;3;3;3" font-underline="0" box-height="94" font="Cambria" letter-spacing="0" font-pixel-size="80" font-italic="0" typewriter="0;2;1;0;0" alignment="4" font-weight="50" box-width="91" font-color="0,255,255,255">MIA!</content>
     </item>
     <item type="QGraphicsTextItem" z-index="7">
      <position x="1106" y="552">
       <transform>1,0,0,0,1,0,0,0,1</transform>
      </position>
      <content line-spacing="0" shadow="0;#64000000;3;3;3" font-underline="0" box-height="94" font="Cambria" letter-spacing="0" font-pixel-size="80" font-italic="0" typewriter="0;2;1;0;0" alignment="4" font-weight="50" box-width="91" font-color="255,255,255,255">6.5</content>
     </item>
    

    要更改输入文件,如果您使用 GNU awk,则更改为awk或awk -i inplace使用任何 awk 更改' "$1"为' "$1" > tmp && mv tmp "$1"。

    • 1
  3. Chris Davies
    2024-06-09T02:06:54+08:002024-06-09T02:06:54+08:00

    答案从问题中移出


    自己用 sed 找到了解决方案:

    sed -i "/$word/ {N; N; N; /MIA!/ s/$word/$final/g}" "$@"
    

    首先,sed 使用在变量中找到的值来搜索坐标xcoord。然后,如果在 3 行之后出现该单词MIA!,它将用在变量中找到的总和数字替换该值final。

    #!/bin/bash
    xcoord=`perl -ne 'push @lines,$_; print $lines[0] if /MIA!/; shift(@lines) if $.>3;' "$@"  | cut -d'"' -f2`
    for word in $xcoord
    do
    final=$(( $word + 40 ))
    sed -i "/$word/ {N; N; N; /MIA!/ s/$word/$final/g}" "$@"
    done
    
    • -1

相关问题

  • 通过命令的标准输出以编程方式导出环境变量[重复]

  • 从文本文件传递变量的奇怪问题

  • 虽然行读取保持转义空间?

  • `tee` 和 `bash` 进程替换顺序

  • 运行一个非常慢的脚本直到它成功

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    模块 i915 可能缺少固件 /lib/firmware/i915/*

    • 3 个回答
  • Marko Smith

    无法获取 jessie backports 存储库

    • 4 个回答
  • Marko Smith

    如何将 GPG 私钥和公钥导出到文件

    • 4 个回答
  • Marko Smith

    我们如何运行存储在变量中的命令?

    • 5 个回答
  • Marko Smith

    如何配置 systemd-resolved 和 systemd-networkd 以使用本地 DNS 服务器来解析本地域和远程 DNS 服务器来解析远程域?

    • 3 个回答
  • Marko Smith

    dist-upgrade 后 Kali Linux 中的 apt-get update 错误 [重复]

    • 2 个回答
  • Marko Smith

    如何从 systemctl 服务日志中查看最新的 x 行

    • 5 个回答
  • Marko Smith

    Nano - 跳转到文件末尾

    • 8 个回答
  • Marko Smith

    grub 错误:你需要先加载内核

    • 4 个回答
  • Marko Smith

    如何下载软件包而不是使用 apt-get 命令安装它?

    • 7 个回答
  • Martin Hope
    user12345 无法获取 jessie backports 存储库 2019-03-27 04:39:28 +0800 CST
  • Martin Hope
    Carl 为什么大多数 systemd 示例都包含 WantedBy=multi-user.target? 2019-03-15 11:49:25 +0800 CST
  • Martin Hope
    rocky 如何将 GPG 私钥和公钥导出到文件 2018-11-16 05:36:15 +0800 CST
  • Martin Hope
    Evan Carroll systemctl 状态显示:“状态:降级” 2018-06-03 18:48:17 +0800 CST
  • Martin Hope
    Tim 我们如何运行存储在变量中的命令? 2018-05-21 04:46:29 +0800 CST
  • Martin Hope
    Ankur S 为什么 /dev/null 是一个文件?为什么它的功能不作为一个简单的程序来实现? 2018-04-17 07:28:04 +0800 CST
  • Martin Hope
    user3191334 如何从 systemctl 服务日志中查看最新的 x 行 2018-02-07 00:14:16 +0800 CST
  • Martin Hope
    Marko Pacak Nano - 跳转到文件末尾 2018-02-01 01:53:03 +0800 CST
  • Martin Hope
    Kidburla 为什么真假这么大? 2018-01-26 12:14:47 +0800 CST
  • Martin Hope
    Christos Baziotis 在一个巨大的(70GB)、一行、文本文件中替换字符串 2017-12-30 06:58:33 +0800 CST

热门标签

linux bash debian shell-script text-processing ubuntu centos shell awk ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve