简体   繁体   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)

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.

相关问题 如何使用AWK编写包含给定单词并在一行中至少具有给定数量字符的文件的每一行? - 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? 将整个单词替换为 AWK - Replace the whole word with AWK 找到一个单词作为模式给出另一个单词,awk,perl,sed,grep? - Found a line-word given as a pattern another line-word, awk, perl, sed, grep? 如何使用 awk 打印整个字符串中的每个单词? - How to use awk to print every word from a whole string? 使用AWK将每个单词放在文本文件中的新行上 - Using AWK to place each word in a text file on a new line 如何在一个特定行的单词之后附加我刚刚从“ awk”命令获得的输出? - How to append an output that I just got from “awk” command after a word of a specific line? 如何打印 word get from variable in specific line with AWK? - How to print word get from variable in specific line with AWK? 在 linux 中用 sed 或 awk 将第一行中的一个单词替换为第二行中的另一个单词 - To replace one word in first line with another in 2nd line in linux with sed or awk 使用awk从另一个文件中的一个文件中搜索多字样式 - searching multi-word patterns from one file in another using awk 如何使用 awk 显示包含特定单词的文件列 - How to display file columns containing a specific word using awk
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM