【发布时间】:2017-11-15 17:50:37
【问题描述】:
本周我正在学习如何使用 awk 和 sed。我知道这个问题之前可能已经被问过,但我不确定我的脚本有什么问题。我有三个文件,我正在使用 grep 搜索模式 gge0001x gge0001y gge0001z。 x 在文件 1 中,y 在文件 2 中,z 在文件 3 中。如果有人想看 L2E[1-3].iva,他们在这里:https://gist.github.com/anonymous/1112988408874c730cd4f3d313226ba4
#!/bin/bash
echo "Performance Data"
sed -n '1,19p' L2E1.iva|cat > file1 #take lines 1-19 in L2E1 and take the
# output into file1. The next two commands do the same thing
sed -n '1,19p' L2E2.iva|cat > file2
sed -n '1,19p' L2E3.iva|cat > file3
curveName=`grep "F" file1|sed "s/F/ /"`
# This will search for F in file 1, and then substitute F with a space
curveName2=`grep "F" file2|sed "s/F/ /"`
curveName3=`grep "F" file3|sed "s/F/ /"`
echo "Curve Name" "$curveName $curveName2 $curveName3"
我希望我的输出是曲线名称 gge0001x gge0001y gge0001z。但输出是这样的: 性能数据 gge0006ze gge0006x
如果我自己回显它们,那很好,但是一旦我在同一行上回显所有三个,输出就会出现偏差。当我回显它时,为什么 x 在第一次出现时最后出现,我的 y 去了哪里?
【问题讨论】:
-
也许你在运行多个命令时需要
&&,并且当你的替换字符时,它不会将更改保存到文件中,你知道的,对吧? -
我试过了,它给了我以下信息:性能数据曲线名称 gge0006x ./lab5.2: line 19: $'gge0006y\r': command not found
-
您的文件中有
\r个字符。在您的输入文件上运行 dos2unix。 -
永远不要将
grep传递给sed。而不是grep foo | sed 'cmd',只需执行sed '/foo/cmd' -
你应该使用
sed 'y/F/ /',而不是sed 's/F/ /',但你应该使用tr F ' '