小编Ode*_*Ode的帖子

将换行符附加到shell脚本中的输出文件

我有一个我在Cygwin中执行的shell脚本(也许这就是问题所在).对于这段代码,我只想写第一行,并附加一个换行符:

echo "`date` User `whoami` started the script." >> output.log
echo >> output.log
Run Code Online (Sandbox Code Playgroud)

但是output.log文件似乎永远不会中断.如果我多次运行脚本,就好像第二个echo没有写入文件.

我也尝试过:

echo -e "`date` User `whoami` started the script.\n" >> output.log
Run Code Online (Sandbox Code Playgroud)

它产生相同的结果.

奇怪的是,如果我只是在命令行上输入上面的第二个echo语句,而不附加到文件,它会给我带有尾随换行符的预期输出.

bash shell cygwin append

28
推荐指数
4
解决办法
11万
查看次数

在shell脚本中优化grep(或使用AWK)

在我的shell脚本中,我试图使用$ sourcefile中找到的术语一遍又一遍地搜索相同的$ targetfile.

我的$ sourcefile格式如下:

pattern1
pattern2
etc...
Run Code Online (Sandbox Code Playgroud)

我必须搜索的低效循环是:

for line in $(< $sourcefile);do
    fgrep $line $targetfile | fgrep "RID" >> $outputfile
done
Run Code Online (Sandbox Code Playgroud)

我知道可以通过将整个$ targetfile加载到内存中,或者使用AWK来改进这一点吗?

谢谢

shell awk grep

4
推荐指数
1
解决办法
1335
查看次数

标签 统计

shell ×2

append ×1

awk ×1

bash ×1

cygwin ×1

grep ×1