【问题标题】:Grep 6th column of a row in a file and replace it with stringGrep 文件中行的第 6 列并将其替换为字符串
【发布时间】:2021-09-07 09:15:50
【问题描述】:

我有一个具有如下 ddl 的文件。现在以(如果不存在 tbl1 时创建表)开头的行中的第 6 列是表名。我想用 DB.table 名称替换表名。 并且在具有位置的行中,它应该填充 $tbl 作为表名。下面是示例输入和输出。

    Create table if not exists tbl1 (
    col1 int;
    col2 string )
location hdfs://directory/$tbl
    Create table if not exists tbl2 (
    col3 int;
    col2=4 float)
location hdfs://directory/$tbl

替换后的输出应该变成下面这样

    Create table if not exists DB.tbl1 (
    col1 int;
    col2 string )
location hdfs://directory/tbl1
    Create table if not exists DB.tbl2 (
    col3 int;
    col2=4 float)
location hdfs://directory/tbl2

任何人都可以建议如何实现这一目标

【问题讨论】:

    标签: linux shell unix scripting


    【解决方案1】:

    sed - 用于文本替换的强大 linux 命令。

    sed 's/tbl/DB.tbl/' filename - 将 all 的 'tbl' 替换为 'DB.tbl' 并达到想要的结果。 (小心使用)。

    延伸阅读:https://www.geeksforgeeks.org/sed-command-in-linux-unix-with-examples/

    awk 也可能有帮助:

    grep "Create" filename | awk '{print $6}' - 将打印文件名中带有单词“Create”的第 6 列行。

    祝你好运。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-01-09
      • 2015-11-26
      • 2022-11-20
      • 1970-01-01
      • 1970-01-01
      • 2013-12-27
      • 1970-01-01
      相关资源
      最近更新 更多