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 / 问题 / 738562
Accepted
Brad Parks
Brad Parks
Asked: 2023-03-04 00:18:42 +0800 CST2023-03-04 00:18:42 +0800 CST 2023-03-04 00:18:42 +0800 CST

bash 的简单模板引擎,可以使用 csv 数据吗?

  • 772

我经常想快速获取一些数据,并将其应用于 bash 中的模板。

例如想象做以下事情

  $ seq 1 2 | eztemplate 'this is a {1} test'
  this is a 1 test
  this is a 2 test

  $ eztemplate 'this is a {1} test' hi 'there now'
  this is a hi test
  this is a there now test

  $ eztemplate /tmp/template /tmp/alphabet  | head
  this is a a test
  of this system a
  this is a b test
  of this system b
  ...

我已经编写了一个非常简单的 bash 脚本来执行此操作,但正在考虑允许每行数据使用多个参数,例如 CSV 样式数据。

鉴于以下情况,是否已经存在比我的小脚本更好的东西?

  • 我希望它只需要基本的 unix posix 工具,以及通常安装的东西,如 perl 或 awk,但不需要 perl 额外安装模块,例如
  • 它可以接受数据文件中每行数据的多列数据。
  • 基本上是一个不需要安装任何其他东西的 bash 脚本 :D
  • 一个侧面的目的是让不擅长 bash 脚本的其他人有一个简单的工具来处理重复数据的模板

数据和模板会有很大差异,但我想做的第一个例子是将 4 个 id 应用于 JSON 负载

模板

{
  "tenantId": "{1}",
  "key": "some.key",
  "value": "some.value"
}

数据

my/super/01cbf4e9779649038f0bd753747c8b26
my/test/01cbf4e9779649038f0bd753747c8b26
ez/test/01cbf4e9779649038f0bd753747c8b26
rad/data/df3a47fed39d453f836f9b2196332029

ez模板

#!/usr/bin/env bash
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"; PATH="$DIR:$PATH"

function show_help()
{
  ME=$(basename "$0")
  IT=$(cat <<EOF

  replaces vars in a template, one for each line of stdin

  e.g.

  $ seq 1 2 | $ME 'this is a {1} test'
  this is a 1 test
  this is a 2 test

  $ $ME 'this is a {1} test' hi 'there now'
  this is a hi test
  this is a there now test

  $ $ME /tmp/template /tmp/alphabet
  this is a a test
  of this system a
  this is a b test
  of this system b
  ...

EOF
)
  echo "$IT"
  echo
  exit
}

if [ -z "$1" ]
then
  show_help
fi
if [ "$1" = "help" ] || [ "$1" = '?' ] || [ "$1" = "--help" ] || [ "$1" = "h" ]; then
  show_help
fi

function process_template(){
  DATA=$1
  VAR=$2

  if [ -f "$DATA" ]; then
    DATA=$(cat "$DATA")
  fi

  echo "$DATA" | sed "s#{1}#$VAR#g"
}


TEMPLATE=$1
if [ -t 0 ]
then
  if [ -f "$2" ]; then
    # allow first 2 parameters to be files, TEMPLATE and then DATA
    DATA_FILE=$2
    cat "$DATA_FILE" | while read line
    do
      process_template "$TEMPLATE" "$line"
    done 
  else
    shift;
    for line in "$@" 
    do
      process_template "$TEMPLATE" "$line"
    done
  fi
else
  # loop over lines from stdin
  while IFS= read -r line; do
    process_template "$TEMPLATE" "$line"
  done
fi
bash
  • 1 1 个回答
  • 68 Views

1 个回答

  • Voted
  1. Best Answer
    J_H
    2023-03-04T01:52:23+08:002023-03-04T01:52:23+08:00

    对于您引用的示例,最自然的解决方案似乎是

    $ seq 1 2 | xargs -n1 printf 'this is a %s test\n'
    

    如果需要,显然awk也等于任务。

    • 3

相关问题

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

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

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

  • `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