[英]How to replace every word with another word from a file using awk ( the words are given as command line parameters in awk)
I did something like this but nothing is printing我做了这样的事情,但没有打印
#!/bin/bash
if [ $# -eq 0 ]
then
echo 'There are no words'
exit 1
fi
echo | awk 'BEGIN {gsub(/ARGV[1]/,"ARGV[2]"); print}' $@
I think it is because I don't have the file specified but how can I specify it if I already read the words from the command line input in awk我认为这是因为我没有指定文件,但是如果我已经从 awk 中的命令行输入中读取了单词,我该如何指定它
You can do something like this你可以做这样的事情
#!/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}' "$@"
if you are using the arguments to awk
that are not filenames you have to remove them.如果您使用的不是文件名的 arguments 到awk
,则必须删除它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.