如何将文件名和文件内容传递给awk

3

简述:

有人知道如何将文件名和文件内容传递给awk吗?并使它运行所有目录中的文件,并将这些操作的输出附加到1个最终文件中吗?

详述:

每周我需要基于2个变量生成一个SQL更新文件,以前我经常复制粘贴很多内容到CSV文件中才能实现这个awk命令。我的设置如下:

一个非常长的手动拼接的CSV,第一行看起来像这样:

3afb6dad-352d-4c2a-b348-40fdb3c3d9a6;019f08dd-5017-43a1-b65b-c77cb90068ab

一个遍历 CSV 的 AWK 命令:

cat list.csv  | awk -F\; '{print "update db01.CONTENT set locationid = \"" $1 "\" where cdbid = \"" $2 "\";";'}

我希望自动化这一过程,通过自动生成CSV文件甚至更好的方式是直接将正确的变量传递给脚本。
我有几个输入文件。在我的awk命令中,文件的标题必须为$1,并且是不变的。文件本身包含数量可变的UUIDs,需要是$2

输入

我有一个名为3afb6dad-352d-4c2a-b348-40fdb3c3d9a6的文件。该文件的内容如下所示:
019f08dd-5017-43a1-b65b-c77cb90068ab 
0479c914-6988-4038-ac74-f5b4adb123d0 
05a6b05a-dff9-4f7c-8a7e-92c8651b8cde 
05ad4a6a-e2c6-4074-adfd-0899c15a3600 
204b12af-42d8-48a0-83c6-10e02a051ed5 
20c4fb93-6ed2-4dee-87da-749b52c76d74 
27b2552a-1050-47fb-96fe-714b4231a067 
343f34be-b1cf-4cdf-8c35-344847a13837

我有另一个名为 72d799e8-ff97-4388-a498-47badd6ca7d8 的文件,其中包含类似于以下内容:

54b0623f-b5f0-47a1-bf90-9c8cb2054676 
8056e400-b809-4e08-bf0a-d5370f3e1b44

期望的输出

我需要得到一个包含以下内容的.sql文件:

 update db01.CONTENT set locationid="3afb6dad-352d-4c2a-b348-40fdb3c3d9a6" where cdbid ="019f08dd-5017-43a1-b65b-c77cb90068ab"; 
update db01.CONTENT set locationid ="3afb6dad-352d-4c2a-b348-40fdb3c3d9a6" where cdbid="0479c914-6988-4038-ac74-f5b4adb123d0"; 
update db01.CONTENT set locationid ="3afb6dad-352d-4c2a-b348-40fdb3c3d9a6" where cdbid="05a6b05a-dff9-4f7c-8a7e-92c8651b8cde"; 
update db01.CONTENT set locationid ="3afb6dad-352d-4c2a-b348-40fdb3c3d9a6" where cdbid="05ad4a6a-e2c6-4074-adfd-0899c15a3600"; 
update db01.CONTENT set locationid ="3afb6dad-352d-4c2a-b348-40fdb3c3d9a6" where cdbid="204b12af-42d8-48a0-83c6-10e02a051ed5"; 
update db01.CONTENT set locationid ="3afb6dad-352d-4c2a-b348-40fdb3c3d9a6" where cdbid="20c4fb93-6ed2-4dee-87da-749b52c76d74"; 
update db01.CONTENT set locationid ="3afb6dad-352d-4c2a-b348-40fdb3c3d9a6" where cdbid="27b2552a-1050-47fb-96fe-714b4231a067"; 
update db01.CONTENT set locationid ="3afb6dad-352d-4c2a-b348-40fdb3c3d9a6" where cdbid="343f34be-b1cf-4cdf-8c35-344847a13837"; 
update db01.CONTENT set locationid ="72d799e8-ff97-4388-a498-47badd6ca7d8" where cdbid="54b0623f-b5f0-47a1-bf90-9c8cb2054676"; 
update db01.CONTENT set locationid ="72d799e8-ff97-4388-a498-47badd6ca7d8" where cdbid="8056e400-b809-4e08-bf0a-d5370f3e1b44";

我尝试了一些方法来组合

for file in ./output/*;
do
  echo ${file##*/}
done

并且

while IFS='' read -r line || [[ -n "$line"]];
do
  #awk stuff
done <"$1"

但是我没有得到任何结果。非常感谢帮助!


1
欢迎来到本网站!请查看tourhow-to-ask page,了解更多关于提问以吸引高质量回答的信息。您可以编辑您的问题以包含更多信息(尽管我非常感谢您目前的详细描述!)。您是否接受使用Perl或其他工具? - cxw
{btsdaf} - John Bollinger
4个回答

1
一种使用纯 的解决方案如下:

#!/bin/bash

cd ./output
for file in *; do
    while read -r line; do
        echo 'update db01.CONTENT set locationid="'$file'" where cdbid ="'$line'";'
    done < $file
done

输出:

update db01.CONTENT set locationid="3afb6dad-352d-4c2a-b348-40fdb3c3d9a6" where cdbid ="019f08dd-5017-43a1-b65b-c77cb90068ab";
update db01.CONTENT set locationid="3afb6dad-352d-4c2a-b348-40fdb3c3d9a6" where cdbid ="0479c914-6988-4038-ac74-f5b4adb123d0";
update db01.CONTENT set locationid="3afb6dad-352d-4c2a-b348-40fdb3c3d9a6" where cdbid ="05a6b05a-dff9-4f7c-8a7e-92c8651b8cde";
update db01.CONTENT set locationid="3afb6dad-352d-4c2a-b348-40fdb3c3d9a6" where cdbid ="05ad4a6a-e2c6-4074-adfd-0899c15a3600";
update db01.CONTENT set locationid="3afb6dad-352d-4c2a-b348-40fdb3c3d9a6" where cdbid ="204b12af-42d8-48a0-83c6-10e02a051ed5";
update db01.CONTENT set locationid="3afb6dad-352d-4c2a-b348-40fdb3c3d9a6" where cdbid ="20c4fb93-6ed2-4dee-87da-749b52c76d74";
update db01.CONTENT set locationid="3afb6dad-352d-4c2a-b348-40fdb3c3d9a6" where cdbid ="27b2552a-1050-47fb-96fe-714b4231a067";
update db01.CONTENT set locationid="3afb6dad-352d-4c2a-b348-40fdb3c3d9a6" where cdbid ="343f34be-b1cf-4cdf-8c35-344847a13837";
update db01.CONTENT set locationid="72d799e8-ff97-4388-a498-47badd6ca7d8" where cdbid ="54b0623f-b5f0-47a1-bf90-9c8cb2054676";
update db01.CONTENT set locationid="72d799e8-ff97-4388-a498-47badd6ca7d8" where cdbid ="8056e400-b809-4e08-bf0a-d5370f3e1b44";

{btsdaf} - Willem Dauwen

1
awk来拯救!根据您的输入/输出文件格式。
$ awk '{file=FILENAME; sub(".*/", "", file);
        print "update db01.CONTENT set locationid=\"" file 
              "\" where cdbid=\"" $1 "\""}' output/*

0

不是 awk,但这里有一个 一行命令:

 perl -ne 'chomp; print "update db01.CONTENT set locationid=\"$ARGV\" where cdbid=\"$_\";\n";' * > whatever.sql

测试

我创建了一个测试目录,其中包含以下两个文件:

in1:(类似于3afb6dad-352d-4c2a-b348-40fdb3c3d9a6

out1         (analogous to 019f08dd-5017-43a1-b65b-c77cb90068ab)
out2         (analogous to 0479c914-6988-4038-ac74-f5b4adb123d0)

in2:(类似于 72d799e8-ff97-4388-a498-47badd6ca7d8

out3         (analogous to 54b0623f-b5f0-47a1-bf90-9c8cb2054676)
out4         (analogous to 8056e400-b809-4e08-bf0a-d5370f3e1b44)

当我在Perl 5.22上运行以上代码时,我得到以下输出:
update db01.CONTENT set locationid="in1" where cdbid="out1";
update db01.CONTENT set locationid="in1" where cdbid="out2";
update db01.CONTENT set locationid="in2" where cdbid="out3";
update db01.CONTENT set locationid="in2" where cdbid="out4";

我认为这类似于你想要的。

解释

编辑 -n 开关提供了一个 while(<>){ ... },围绕着给定的命令。因此,Perl 逐行处理命令行上给定的每个文件。在脚本中(隐式循环体内),$_ 是该行,$ARGV 是它来自的文件名(reference)。chomp$_ 中删除尾随换行符,然后 print 语句输出所需的 SQL。所有输出都由 shell 保存到 whatever.sql 中。


{btsdaf} - Gilles Quénot
{btsdaf} - cxw

0

正如我在评论中所写的那样,仅为了使用awk而创建CSV中间文件是相当无意义的。如果您可以创建这样的文件,那么您也可以直接创建所需的SQL脚本。例如:

#!/bin/bash

for file in ./output/*;
do
  location=$(basename "$file")
  while read cb <"$file"; do
    echo "update db01.CONTENT set locationid=\"${location}\" where cdbid =\"${cb}\";"
  done
done

该版本将结果写入标准输出。您可以将其重定向到任何位置,或者将所需的重定向放入脚本中。


{btsdaf} - Willem Dauwen

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接