日志文件的内容如下,
I, [2018-03-07T10:21:26.315447 #7531] INFO -- : POST http://127.0.0.1:8080/api/v3/internal/allowed 6.69490
I, [2018-03-07T10:21:26.315799 #7531] INFO -- : gitlab-shell: executing git command <git-upload-pack /var/opt/gitlab/git-data/repositories/GitLab-write-logs/write-logs.git> for user with key key-1.
I, [2018-03-07T10:23:00.567841 #7739] INFO -- : POST http://127.0.0.1:8080/api/v3/internal/allowed 5.55714
I, [2018-03-07T10:23:00.568227 #7739] INFO -- : gitlab-shell: executing git command <git-upload-pack /var/opt/gitlab/git-data/repositories/GitLab-read-logs/read-logs.git> for user with key key-3.
I, [2018-03-07T10:23:49.932948 #7851] INFO -- : gitlab-shell: executing git command <git-receive-pack /var/opt/gitlab/git-data/repositories/GitLab-RW-logs/RW-logs.git> for user with key key-5.
I, [2018-03-07T10:55:29.533385 #4778] INFO -- : gitlab-shell: executing git command <git-receive-pack /var/opt/gitlab/git-data/repositories/GitLab-write-logs/write-logs.git> for user with key key-1.
I, [2018-03-07T10:55:29.810478 #4790] INFO -- : POST http://127.0.0.1:8080/api/v3/internal/allowed 0.10971
模式=&lt; GIT-上传 -
这种匹配模式有多条线。如何从每一行中提取信息。
例如,在第二行中我们有匹配模式&lt; git-upload - 然后我想从该行提取字符串'/ var / opt / gitlab / git-data / repositories / GitLab-write-logs / write-logs 并分配变量并做一些动作。
我希望按顺序对所有匹配的行进行此过程,而不重复相同的行,我该怎么做?我感谢大家的意见
假设我们有50行,其中包含匹配的图案线。我们可以一次grep一行做行动并拿起下一行并重复相同的行。
逻辑:
if more somefile.log | grep -Eq < git-upload-pack ; then
variable = /var/opt/gitlab/git-data/repositories/GitLab-write-logs/write-logs [ .git shouldn't be there]
do some action
fi
我应该把while循环(i = 0,i ++)重复每一行的过程,比如..检查行,如果模式然后执行“if”条件并退出条件并重复下一行我是新手编写脚本并努力连接点
谢谢大家
答案 0 :(得分:0)
只要格式可预测,听起来就像是grep和cut的工作:
$grep git-upload-pack t | cut -d " " -f 13 | cut -d ">" -f 1
/var/opt/gitlab/git-data/repositories/GitLab-write-logs/write-logs.git
/var/opt/gitlab/git-data/repositories/GitLab-read-logs/read-logs.git
grep git-upload-pack
命令查找包含git-upload-pack cut -d " " -f 13
抓住第13个以空格分隔的字段cut -d ">" -f 1
抓住2个字段中的第1个字段,有效删除了&#34;&gt;&#34; 循环结果输出:
$grep git-upload-pack t |
cut -d " " -f 13 |
cut -d ">" -f 1 |
while read f; do echo your-command ${f%.*}; done
你可以在哪里取代&#34;你的命令&#34;无论你想做什么与每场比赛,并删除回声实际运行它。尝试使用回声,直到它有意义。
How can remove the extension of a filename in a shell script?解释了$ {f%。*}以删除.git。
答案 1 :(得分:-1)
使用gnu sed
sed '/<git-upload-/!d;s/.*<git-upload-[^/]*\([^>]*\).*/echo \1 | tr [:lower:] [:upper:] /e' infile