Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash:将单词替换为列表中的另一个单词_Bash_Sed - Fatal编程技术网

Bash:将单词替换为列表中的另一个单词

Bash:将单词替换为列表中的另一个单词,bash,sed,Bash,Sed,我尝试编写一个脚本,根据列表文件,用另一个对应的单词替换文件中的每个单词 phylofile(要修改的文件)是: namefile(从旧单词到新单词的映射列表)是: 输出应为: (((papilio,catocala):90,pyraloid):74,orocrambus) 我希望这样更清楚 我写了以下脚本: echo -n "Enter the path to the file where names should be changed: " read phylofile echo -n "

我尝试编写一个脚本,根据列表文件,用另一个对应的单词替换文件中的每个单词

phylofile
(要修改的文件)是:

namefile
(从旧单词到新单词的映射列表)是:

输出应为:

(((papilio,catocala):90,pyraloid):74,orocrambus)
我希望这样更清楚

我写了以下脚本:

echo -n "Enter the path to the file where names should be changed: "
read phylofile
echo -n "Enter the path to the file containing the string searched and the replacing string: "
read namefile

while read var
do
    searchstring=`echo "$var"|awk -F= '{print $1}'`
    replacestring=`echo "$var"|awk -F= '{print $2}'`
    sed "s/$searchstring/$replacestring/g" $phylofile > outputfile
done < $namefile
echo-n“输入要更改名称的文件路径:”
读取文件
echo-n“输入包含搜索字符串和替换字符串的文件路径:”
读取名称文件
读var时
做
searchstring=`echo“$var”| awk-F='{print$1}'`
replacestring=`echo“$var”| awk-F='{print$2}'`
sed“s/$searchstring/$replacestring/g”$phylofile>outputfile
完成<$namefile
我收到一条错误消息(法语),表示sed命令中没有正则表达式


如果您能提供帮助,我将不胜感激。

像这样的东西就是您所需要的:

echo -n "Enter the path to the file where names should be changed: "
read phylofile
echo -n "Enter the path to the file containing the string searched and the replacing string: "
read namefile

awk '
NR==FNR{ map["\\<"$1"\\>"]=$2; next }
{
    for (old in map) {
        new = map[old]
        gsub(old,new)
    }
    print
}
' "$namefile" "$phylofile"
下面是在它们上运行的awk脚本:

$ awk '
NR==FNR{ map["\\<"$1"\\>"]=$2; next }
{
    for (old in map) {
        new = map[old]
        gsub(old,new)
    }
    print
}
' namefile phylofile
(((papilio,catocala):90,pyraloid):74,orocrambus)
$awk'
NR==FNR{map[“\\”]=$2;next}
{
用于(地图中的旧版本){
新=地图[旧]
gsub(旧、新)
}
打印
}
'名称文件
((帕皮利奥,卡托卡拉):90,吡咯烷酸):74,奥罗巴)

事实上,塞勒斯是对的。您应该了解如何在问题框中设置源代码的格式。不过,你是新来的,我这次是为你做的。回到问题上来,您可能已经在这里看到了:您可以在sed中做任何事情,为什么要使用awk?错误的问题。除了一些琐碎的操作之外,您可以而且应该使用awk执行所有UNIX文本操作,那么为什么要使用shell和sed呢?谢谢您的回答。是的,我很确定我可以用sed来做,但是我没有发现如何..sed是对单个行的简单替换,这不是,所以你不能用sed来做。您可以在shell和sed的混合中实现这一点,但要可靠地编写代码是非常困难的(例如,您的
while read var
通常会损坏您正在阅读的内容-您需要在
var
保证包含文件中存在的值之前添加其他构造)因为这是一种完全错误的方法,就像shell处理文件和进程以及对工具调用排序一样。发明shell和sed的人也发明了awk来做这样的通用文本处理,所以使用它吧。谢谢你,但是这个脚本没有提供我需要的。在文件中:((燕尾,noctuid):90,吡咯烷酮):74,crambine)我想替换如下名称:crambine或crambus燕尾papilio noctuid catocala生成的文件将是(((papilio,catocala):90,pyraloid):74,orocrambus)我希望它更清楚,好吧,那么脚本以什么方式不
提供您需要的内容
?错误的输出,没有输出,核心转储,错误消息,其他什么?不要试图将格式化文本放入注释中,编辑您的问题以包含代表性输入(namefile和phylofile的内容)和预期输出。脚本似乎可以工作(没有错误消息),但它不会返回任何输出。哦,是的,它缺少我现在添加的
打印
语句。正如你所看到的,你永远不应该仅仅说一个提议的解决方案“不起作用”-始终要清楚具体地说明它以什么方式不起作用,这样我们就可以帮助你找出如何解决它。嗯。。。我很惭愧地承认它没有安装(但我在其他脚本中使用了awk,它还在工作,不知道我应该安装gawk)。它正在工作!谢谢你抽出时间!
echo -n "Enter the path to the file where names should be changed: "
read phylofile
echo -n "Enter the path to the file containing the string searched and the replacing string: "
read namefile

awk '
NR==FNR{ map["\\<"$1"\\>"]=$2; next }
{
    for (old in map) {
        new = map[old]
        gsub(old,new)
    }
    print
}
' "$namefile" "$phylofile"
$ cat namefile
crambine orocrambus
swallowtail papilio
noctuid catocala

$ cat phylofile
(((swallowtail,noctuid):90,pyraloid):74,crambine)
$ awk '
NR==FNR{ map["\\<"$1"\\>"]=$2; next }
{
    for (old in map) {
        new = map[old]
        gsub(old,new)
    }
    print
}
' namefile phylofile
(((papilio,catocala):90,pyraloid):74,orocrambus)