我需要在shell脚本中使用以下grep命令:
grep 'DUE TO TIME LIMIT' ./slurm-3364.out
其中输出slurm-3364.out中包含“DUE TO TIME LIMIT”模式的行。我想用变量替换文件名,并将输出影响到另一个变量。到目前为止,我无法得到相同的结果,我试过了:
jobOver="$(grep 'DUE TO TIME LIMIT' "$NEWEST")"
echo $jobOver
使用$ NEWEST包含相应的文件名。这会产生一个很长的文本输出,似乎列出了工作目录中的每个文件......
编辑: 正如评论中所提到的,做回声“$ jobOver”解决了这个问题。
答案 0 :(得分:2)
您可以执行以下操作:
jobOver=$(grep 'DUE TO TIME LIMIT' "$NEWEST")
echo "$jobOver"
此处,NEWEST
是包含您想要grep的文件名的变量。
这是我刚刚运行的本地测试:
$ cat slurm-3364.out
I DIED DUE TO TIME LIMIT, YESTERDAY.
I FEEL GREAT WHERE I AM.
YOU SHOULD JOIN ME.
$ NEWEST="slurm-3364.out"
$ jobOver=$(grep 'DUE TO TIME LIMIT' "$NEWEST")
$ echo "$jobOver"
I DIED DUE TO TIME LIMIT, YESTERDAY.