简体   繁体   English

bash脚本中的awk脚本

[英]awk script in bash script

I want to use a awk-script inside of an bash-script. 我想在bash脚本中使用awk脚本。 Somehow it doesn't work right. 不知何故,它无法正常工作。

This is my bash script: 这是我的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"

and this is my awk script 这是我的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

Right now the bash script just takes the awk-script and writes it in front of the input-file. 现在,bash脚本仅采用awk脚本并将其写入输入文件的前面。

Edit: I found an answer. 编辑:我找到了答案。

changed the bash script to: 将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"

and removed the -f in the awk-script 并在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

To answer the original question (how to run an awk script inside a bash script), here is an example how you embed an awk script into your bash code: 为了回答原始问题(如何在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