将输出作为新行添加到shell脚本中

时间:2022-01-26 13:53:11

I have a log file log .txt which is having the data in below format

我有一个日志文件log .txt,它具有以下格式的数据

Name=abc Date=20140710
Name=xyz Date=20140715
Name=pqr Date=20140810  And so on

I am fetching the data based on today's date and appending it to a log file in new line

我根据今天的日期获取数据并将其附加到新行的日志文件中

today=$(date --date "+1 week" +%Y%m%d)

grep $today log.txt $'\r' >> append_file.txt

But when I am running the script, it is giving me exception like

但是当我运行脚本时,它给了我异常

: No such file or directory

Also, in the append_file.txt, it is keeping the data as

此外,在append_file.txt中,它将数据保持为

log.txt:Name=abc Date=20140710

Ideally it should keep only the data i.e.

理想情况下,它应该只保留数据,即

Name=abc Date=20140710

Actually, my end point objective is mail the content of append_file.txt and I want the data line wise...... like this

实际上,我的终点目标是邮件append_file.txt的内容,我希望数据行明智......就像这样

Name=abc Date=20140710
Name=mno Date=20140710

At present, it is mailing the data in single line Name=abc Date=20140710 Name=mno Date=20140710

目前,它是以单行邮寄数据名称= abc日期= 20140710名称= mno日期= 20140710

Any suggestion ?

有什么建议吗?

1 个解决方案

#1


1  

Your output looks like:

您的输出如下:

log.txt:Name=abc Date=20140710

Because grep thinks that you're giving more than one file to work with.

因为grep认为你提供了多个文件。

The problem is $'\r' in this line:

问题是$'\ r'在这一行:

grep $today log.txt $'\r' >> append_file.txt

Replace it by:

替换为:

grep $today log.txt >> append_file.txt

or if you need to insert \r at the end of each line:

或者如果您需要在每行的末尾插入\ r \ n:

grep $today log.txt | sed -e 's/$/\r/g' >> append_file.txt

#1


1  

Your output looks like:

您的输出如下:

log.txt:Name=abc Date=20140710

Because grep thinks that you're giving more than one file to work with.

因为grep认为你提供了多个文件。

The problem is $'\r' in this line:

问题是$'\ r'在这一行:

grep $today log.txt $'\r' >> append_file.txt

Replace it by:

替换为:

grep $today log.txt >> append_file.txt

or if you need to insert \r at the end of each line:

或者如果您需要在每行的末尾插入\ r \ n:

grep $today log.txt | sed -e 's/$/\r/g' >> append_file.txt