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
    • 最新
    • 标签
主页 / ubuntu / 问题 / 1408154
Accepted
genderbee
genderbee
Asked: 2022-05-13 04:37:32 +0800 CST2022-05-13 04:37:32 +0800 CST 2022-05-13 04:37:32 +0800 CST

根据 1 行从 2 个文件中插入变量

  • 772

我有 3 个源文件。

1.txt- 模板。

<field name="COL1" label="COL2" data-source="COL1" classes="attribute" category="Attribute"/>

2.txt- COL1 的变量。

IDNName
BusinessUnit
WWB
IncentiveID

3.txt- COL2 的变量。

IDN Name
Business Unit
WWB
Incentive ID

我需要这样的输出(从2.txtand中取出第一行并插入到模板中,然后从and3.txt中取出第二行并插入到模板中,等等...)2.txt3.txt

<field name="IDNName" label="IDN Name" data-source="IDNName" classes="attribute" category="Attribute"/>
<field name="BusinessUnit" label="Business Unit" data-source="BusinessUnit" classes="attribute" category="Attribute"/>
<field name="WWB" label="COL2" data-source="WWB" classes="attribute" category="Attribute"/>
<field name="IncentiveID" label="Incentive ID" data-source="IncentiveID" classes="attribute" category="Attribute"/>

for是否可以通过一个条件来做到这一点sed?我试过了,但我不知道如何将 2 个文件合并为一行。

for i in $(cat 2.txt); do cat 1.txt | sed 's/COL1/'$i'/g'; echo; done

谢谢。

bash
  • 1 1 个回答
  • 21 Views

1 个回答

  • Voted
  1. Best Answer
    steeldriver
    2022-05-13T05:48:19+08:002022-05-13T05:48:19+08:00

    如果您知道 2.txt 或 3.txt 文件中可能不会出现诸如 TAB 之类的“安全”分隔符,那么您可以这样做

    $ paste 2.txt 3.txt | while IFS=$'\t' read -r src lbl; do 
        sed -e 's/"COL1"/"'"$src"'"/g' -e 's/"COL2"/"'"$lbl"'"/' 1.txt
      done
    <field name="IDNName" label="IDN Name" data-source="IDNName" classes="attribute" category="Attribute"/>
    <field name="BusinessUnit" label="Business Unit" data-source="BusinessUnit" classes="attribute" category="Attribute"/>
    <field name="WWB" label="WWB" data-source="WWB" classes="attribute" category="Attribute"/>
    <field name="IncentiveID" label="Incentive ID" data-source="IncentiveID" classes="attribute" category="Attribute"/>
    

    或者使用 GNU 并行:

    $ parallel --link sed -e 's/"COL1"/"{1}"/g' -e 's/"COL2"/"{2}"/' 1.txt :::: 2.txt 3.txt
    <field name="IDNName" label="IDN Name" data-source="IDNName" classes="attribute" category="Attribute"/>
    <field name="BusinessUnit" label="Business Unit" data-source="BusinessUnit" classes="attribute" category="Attribute"/>
    <field name="WWB" label="WWB" data-source="WWB" classes="attribute" category="Attribute"/>
    <field name="IncentiveID" label="Incentive ID" data-source="IncentiveID" classes="attribute" category="Attribute"/>
    

    但是,如果您实际上正在使用 XML 数据,那么您可能想要使用适当的 XML 工具 -xmlstarlet是一种可能性,但由于我更熟悉这里使用yqjq的 kislyuk 的选项:命令行 YAML/XML/TOML处理器 - YAML、XML、TOML 文档的 jq 包装器xq

    $ xq -x --rawfile src 2.txt --rawfile lbl 3.txt '
        foreach ([($src | split("\n")[0:-1]), ($lbl | split("\n")[0:-1])] | transpose[]) as $a
          (.; .field |= (."@name" |= $a[0] | ."@label" |= $a[1] | ."@data-source" |= $a[0]))
    ' 1.txt
    <field name="IDNName" label="IDN Name" data-source="IDNName" classes="attribute" category="Attribute"></field>
    <field name="BusinessUnit" label="Business Unit" data-source="BusinessUnit" classes="attribute" category="Attribute"></field>
    <field name="WWB" label="WWB" data-source="WWB" classes="attribute" category="Attribute"></field>
    <field name="IncentiveID" label="Incentive ID" data-source="IncentiveID" classes="attribute" category="Attribute"></field>
    
    • 3

相关问题

  • 同时复制到两个位置

  • 如何在 shell 脚本中创建选择菜单?

  • 从 bash 迁移到 zsh [关闭]

  • bashrc 还是 bash_profile?

  • 备份 bash 脚本未压缩其 tarball

Sidebar

Stats

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

    如何运行 .sh 脚本?

    • 16 个回答
  • Marko Smith

    如何安装 .tar.gz(或 .tar.bz2)文件?

    • 14 个回答
  • Marko Smith

    如何列出所有已安装的软件包

    • 24 个回答
  • Marko Smith

    无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗?

    • 25 个回答
  • Martin Hope
    Flimm 如何在没有 sudo 的情况下使用 docker? 2014-06-07 00:17:43 +0800 CST
  • Martin Hope
    Ivan 如何列出所有已安装的软件包 2010-12-17 18:08:49 +0800 CST
  • Martin Hope
    La Ode Adam Saputra 无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗? 2010-11-30 18:12:48 +0800 CST
  • Martin Hope
    David Barry 如何从命令行确定目录(文件夹)的总大小? 2010-08-06 10:20:23 +0800 CST
  • Martin Hope
    jfoucher “以下软件包已被保留:”为什么以及如何解决? 2010-08-01 13:59:22 +0800 CST
  • Martin Hope
    David Ashford 如何删除 PPA? 2010-07-30 01:09:42 +0800 CST

热门标签

10.10 10.04 gnome networking server command-line package-management software-recommendation sound xorg

Explore

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

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve