Shell查看文件的最后5行,并对每行进行正则匹配,代码如下: #!/bin/sh pattern="HeartBeat" /home/test/log/log_20150205.log | while read line do if [[ $line =~ $pattern ]] then echo "Match" else echo "No match" fi echo $line done…
写法一: #!/bin/bash while read line do echo $line done < file(待读取的文件) 写法二: #!/bin/bash cat file(待读取的文件) | while read line do echo $line done 写法三: for line in `cat file(待读取的文件)` do echo $line done 说明:for逐行读和while逐行读是有区别的,如: $ cat file aaaa bbbb cccc dddd…
目录 Shell 打印文件的最后5行 题解-awk 题解-tail Shell 打印文件的最后5行 经常查看日志的时候,会从文件的末尾往前查看,于是请你写一个 bash脚本以输出一个文本文件 nowcoder.txt中的最后5行 示例: 假设 nowcoder.txt 内容如下: #include<iostream> using namespace std; int main() { int a = 10; int b = 100; cout << "a + b:&quo…
Shell脚本,执行解释速度快.代码简单易于理解.在shell代码编写过程中,经常会用到读取文件内容. 写法一: ---------------------------------------------------------------------------- #!/bin/bash while read line do echo $line done < file(待读取的文件) ------------------------------------------…