簡體   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