【发布时间】:2020-06-29 15:52:53
【问题描述】:
如果找到匹配并且位置明智,我想替换另一个文件的一些文本。
第一个文件:
abc ram
bdc jay
vlm rock
第二个文件:
grep -f -w '' /d/demo.txt
grep -f -w '' /d/demo.txt
grep -f -w '' /d/demo.txt
如您所见,第一个文件数为 3,第二个文件数为 3。我的输出应该是这样的:
grep -f -w 'abc ram' /d/demo.txt
grep -f -w 'bdc jay' /d/demo.txt
grep -f -w 'vlm rock' /d/demo.txt
第一个文件值的位置应该放在第二个文件的第一个位置,依此类推。
abc ram
grep -f -w 'abc ram' /d/demo.txt
我已经编写了一些代码,但无法构建逻辑.. 最多匹配两个文件计数是在逻辑无法执行的内部完成的。
if [ $count_firstfile == $count_secondfile ]
then
// Logic needed here
elif [ $count_firstfile != $count_secondfile ]
then
else
echo "Nothing to do"
fi
【问题讨论】:
-
请注意,
[ $anything == $anything_else ]存在错误的原因有很多。使用[ "$anything" = "$anything_else" ],引用每个变量,并且只使用一个=而不是==。 (包括 bash 在内的一些 shell 包含一个允许使用==的test的实现,但是让代码只在 bash 而不是像 dash 或 ash 这样的基线 POSIX shell 上工作不是好的做法;省略引号引入各种各样的错误,具体取决于运行时使用的值)。 -
看起来你是writing a script to generate another script。不如我们帮你去掉中间人,让第一个脚本直接运行
grep调用?