linux - 如何使用 bash 中的参数从文件中提取行

标签 linux bash shell unix sh

<分区>

我有一个包含所有信息的日志文件。

2017-09-21 02:01:11,130 [http-nio-8080-exec-5] INFO   - Inbound Message
...
...
2017-09-21 09:01:11,130 [http-nio-8080-exec-5] INFO   - Inbound Message
----------------------------
ID: 6044
Address: http://localhost/serveur/Service?wsdl
Encoding: UTF-8
Http-Method: POST
...
2017-09-21 12:01:11,130 [http-nio-8080-exec-5] INFO   - Inbound Message
...

我只想提取 2 个日期之间的信息。例如,如果我想要 2017-09-21 09.. 和 2017-09-21 12 之间的信息,我会得到这样的信息:

./script.sh 2017-09-21 09 2017-09-21 12
# $1 = 2017-09-21
# $2 = 09
# $3 = 2017-09-21
# $4 = 12
# $1 & 2 are date and hour to begin extract
# $3 & 4 are date and hour to finish extract

我的脚本是这样的。我不了解 shell 编程,但我尝试这样做。不幸的是,它不起作用

#!/bin/bash

beginLine = "$1 $2"
finishLine = "$3 $4"

while read fileLog.log
do
    if($fileLog.log grep beginLine)
         echo $fileLog.log >> extractedlog.log
    fi
    if($fileLog.log grep finishLine)
         < extractedLog.log;
    fi
done
exit 0;

谁能告诉我问题出在哪里?

最佳答案

$ awk '/2017-09-21 09/{a=1};a;/2017-09-21 12/{exit}' input
2017-09-21 09:01:11,130 [http-nio-8080-exec-5] INFO   - Inbound Message
----------------------------
ID: 6044
Address: http://localhost/serveur/Service?wsdl
Encoding: UTF-8
Http-Method: POST
...
2017-09-21 12:01:11,130 [http-nio-8080-exec-5] INFO   - Inbound Message

找到更多替代品 here

关于linux - 如何使用 bash 中的参数从文件中提取行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46706098/

相关文章:

python -/opt/local/lib/node_modules/less/bin/lessc : not found - Django Error

linux - 我怎样才能将文件名保存为这段代码的变量

bash - 将分隔的输出读入多个 Bash 变量

bash - 从键/值对文件设置环境变量

linux - 查找:-exec 缺少参数不复制

linux - 定期列出文件的 Bash 脚本

linux - 目录中的最大 inode 数?

bash - 如何在 unix 中从当前时间减去 60 分钟

python - TAB 自动完成 python CLI

用于启动/关闭 jboss7 的 shell 脚本失败并出现 "Unexpected end of file"语法错误