简体   繁体   English

从日志文件的 sftp 连接中排除特定行

[英]Exclude specific lines from sftp connection from logfile

I have multiple scripts to connect to an sftp server and put/get files.我有多个脚本可以连接到 sftp 服务器并放置/获取文件。 Recently the sftp admins added a large header for all the logins, which has blown up my log files, and I'd like to exclude that as it's blowing up the file sizes as well as making the logs somewhat unreadable.最近 sftp 管理员为所有登录添加了一个大标题,这已经炸毁了我的日志文件,我想排除它,因为它炸毁了文件大小并使日志变得有些不可读。

As is, the commands are of the following format:照原样,命令的格式如下:

sftp user@host <<EOF &>> ${LOGFILE}
get ...
put ...
exit
EOF

Now, I've tried grepping out all the new lines, which all start with a pipe (they basically made a box to put them in).现在,我尝试找出所有以管道开头的新行(他们基本上制作了一个盒子来放入它们)。

sftp user@host <<EOF | grep -v '^|' &>> ${LOGFILE}
get ...
put ...
exit
EOF

This excludes the lines from ${LOGFILE} but throws them to stdout instead which means they end up in another log file, where we also don't want them (these scripts are called by a scheduler).这排除了${LOGFILE}的行,而是将它们抛出到stdout ,这意味着它们最终出现在另一个日志文件中,我们也不想要它们(这些脚本由调度程序调用)。 Oddly, it also seems to filter out the first line from the connection attempt output.奇怪的是,它似乎也从连接尝试输出中过滤掉了第一行。

Connecting to <host>...

and redirect that to stdout as well.并将其重定向到stdout Not the end of the world, but I do find it odd.不是世界末日,但我确实觉得很奇怪。

How can I filter the lines beginning with |如何过滤以|开头的行so they don't show anywhere?所以他们不会在任何地方显示?

In

sftp user@host <<EOF &>> ${LOGFILE}

You are redirecting stdout and stderr to the logfile for appending data (&>>).您正在将 stdout 和 stderr 重定向到日志文件以追加数据 (&>>)。 But when you use但是当你使用

sftp user@host <<EOF | grep -v '^|' &>> ${LOGFILE}

you are only redirecting stdout to grep, leaving the stderr output of sftp to pass untouched.您只是将 stdout 重定向到 grep,而使 sftp 的 stderr 输出保持不变。 Finally, you are redirecting agains stdout and stderr of grep to the logfile.最后,您将 grep 的 stdout 和 stderr 重新重定向到日志文件。

In fact, you are interested in redirecting both (stdout and stderr) from sftp, so you can use someting like:事实上,您有兴趣从 sftp 重定向(stdout 和 stderr),因此您可以使用以下内容:

sftp user@host <<EOF |& grep -v '^|' >> ${LOGFILE}

or, in older versions of bash, using the specific redirection instead of the shorthand:或者,在旧版本的 bash 中,使用特定的重定向而不是速记:

sftp user@host <<EOF 2>&1 | grep -v '^|' >> ${LOGFILE}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM