![](/img/trans.png)
[英]How do I write every line of a file which contains a given word and has at least a given number of characters in a line using 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.