繁体   English   中英

bash脚本中的awk脚本

[英]awk script in bash script

我想在bash脚本中使用awk脚本。 不知何故,它无法正常工作。

这是我的bash脚本:

 1 #!/bin/bash
 2
 3
 4 echo "In what file do you want to look?"
 5 read input
 6 input="1D-MA.mcr"
 7
 8 echo "What are you looking for?"
 9 read muster
10
11 echo "What is the new value?"
12 read newVal
13
14 echo "What is the name of the new file?"
15 read output
16 
18 awk -v muster="${muster}" value="${newVal}" replace.awk "$input" > "$output"

这是我的awk脚本

 1 #!/usr/bin/awk -f
 2
 3 $1 == "$!VARSET" && $2 ~ muster { split($2, tmp, "=");  $2=tmp[1] "=" value; print }
 4 $1 != "$!VARSET" || $2 !~ muster

现在,bash脚本仅采用awk脚本并将其写入输入文件的前面。

编辑:我找到了答案。

将bash脚本更改为:

 1 #!/bin/bash
 2
 3
 4 echo "In what file do you want to look?"
 5 read input
 6 input="1D-MA.mcr"
 7
 8 echo "What are you looking for?"
 9 read muster
10
11 echo "What is the new value?"
12 read newVal
13
14 echo "What is the name of the new file?"
15 read output
16 
18 awk -v muster="${muster}" value="${newVal}" -f replace.awk "$input" > "$output"

并在awk脚本中删除了-f

 1 #!/usr/bin/awk 
 2
 3 $1 == "$!VARSET" && $2 ~ muster { split($2, tmp, "=");  $2=tmp[1] "=" value; print }
 4 $1 != "$!VARSET" || $2 !~ muster

为了回答原始问题(如何在bash脚本中运行awk脚本),下面是一个示例如何将awk脚本嵌入到bash代码中的示例:

#!/bin/bash

echo "This is bash speaking ..."    

awk -f /dev/stdin << EOF    
   BEGIN { print "... and this is actually awk!"; }    
EOF    

echo " ... and now it's me again, bash :-)"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM