Sal*_*n A 3 linux bash shell scripting
我想解析一个包含类似这些行的日志文件(log.txt):
2010-10-19 07:56:14 URL:http://www.website.com/page.php?ID=26 [13676] -> "www.website.com/page.php?ID=26" [1]
2010-10-19 07:56:14 URL:http://www.website.com/page.php?ID=44 [14152] -> "www.website.com/page.php?ID=44" [1]
2010-10-19 07:56:14 URL:http://www.website.com/page.php?ID=13 [13681] -> "www.website.com/page.php?ID=13" [1]
2010-10-19 07:56:14 ERROR:Something bad happened
2010-10-19 07:56:14 ERROR:Something really bad happened
2010-10-19 07:56:15 URL:http://www.website.com/page.php?ID=14 [12627] -> "www.website.com/page.php?ID=14" [1]
2010-10-19 07:56:14 ERROR:Page not found
2010-10-19 07:56:15 URL:http://www.website.com/page.php?ID=29 [13694] -> "www.website.com/page.php?ID=29" [1]
Run Code Online (Sandbox Code Playgroud)
你可能已经猜到了:
1)我需要从每一行中提取这部分:
2010-10-19 07:56:15 URL:http://www.website.com/page.php?ID=29 [13694] -> "www.website.com/page.php?ID=29" [1]
------------------------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Run Code Online (Sandbox Code Playgroud)
2)这部分转到另一个文件(log.html),如下所示:
<a href="http://www.website.com/page.php?ID=29">http://www.website.com/page.php?ID=29</a>
Run Code Online (Sandbox Code Playgroud)
我需要通过bash脚本执行此操作,该脚本将在*nix平台上运行.我不知道shell编程,所以详细的脚本将非常感激,指向bash编程参考的指针会做.
这是一个bash解决方案
#!/bin/bash
exec 4<"log.txt"
while read -r line<&4
do
case "$line" in
*URL:* )
url="${line#*URL:}"
url=${url%% [*}
echo "<a href=\"${url}\">${url}</a>"
esac
done
exec 4<&-
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5650 次 |
| 最近记录: |