我正在运行以下命令:
for line in $(cat file1.txt); do cat file2.txt | grep ${line}; done
它工作正常..但我正在寻找一种方法在这样的文件中打印结果:
$(line) - grep result
有没有办法自定义grep
输出?
由于
答案 0 :(得分:0)
file1="file1.txt";
file2="file2.txt";
for line in $file1; do
result=$(cat $file2 | grep $line | sed -e :a -e N -e '$!ba' -e 's/\n/ /g'));
echo "$line-$result";
done
如果$(cat file2.txt | grep $ line)的结果是多行,你应该用for语句实现echo。
for line in $(file1.txt); do
result=$(cat file2.txt | grep $line);
echo $line
for _line in $result; do
echo " -$_line";
done
done