簡體   English   中英

如何使用 awk 將文件中的每個單詞替換為另一個單詞(單詞在 awk 中作為命令行參數給出)

[英]How to replace every word with another word from a file using awk ( the words are given as command line parameters in awk)

我做了這樣的事情,但沒有打印


#!/bin/bash

if [ $# -eq 0 ]
then
        echo 'There are no words'
        exit 1
fi
echo | awk 'BEGIN {gsub(/ARGV[1]/,"ARGV[2]");  print}' $@

我認為這是因為我沒有指定文件,但是如果我已經從 awk 中的命令行輸入中讀取了單詞,我該如何指定它

你可以做這樣的事情

#!/bin/bash

if [ $# -lt 3 ]
then
        echo "usage: $0 str1 str2 file [file...]"
        exit 1
fi
exec awk 'BEGIN {s=ARGV[1]; r=ARGV[2]; delete ARGV[1]; delete ARGV[2]} {gsub(s,r);  print}' "$@"

如果您使用的不是文件名的 arguments 到awk ,則必須刪除它們。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM