生信喵 发表于 2024-12-12 20:50:49

SHELL2 打印文件的最后5行

<h2>描述</h2>
<p>查看日志的时候,经常会从文件的末尾往前查看,请你写一个bash shell脚本以输出一个文本文件nowcoder.txt中的最后5行。<br />
示例:<br />
假设 nowcoder.txt 内容如下:</p>
<pre><code>#include&lt;iostream&gt;
using namespace std;
int main()
{
int a = 10;
int b = 100;
cout &lt;&lt; &quot;a + b:&quot; &lt;&lt; a + b &lt;&lt; endl;
return 0;
}
</code></pre>
<p>你的脚本应当输出:</p>
<pre><code>int a = 10;
int b = 100;
cout &lt;&lt; &quot;a + b:&quot; &lt;&lt; a + b &lt;&lt; endl;
return 0;
}
</code></pre>

生信喵 发表于 2024-12-12 20:51:12

本帖最后由 生信喵 于 2024-12-12 21:02 编辑

<pre><code>#!/bin/bash
while read line
do
    row=$((row+1))
done&lt;nowcoder.txt
while read line
do
    if [ $row -lt 6 -a $row -gt 0 ]
    then
      echo $line
    fi
row=$((row-1))
done&lt;nowcoder.txt

</code></pre>
页: [1]
查看完整版本: SHELL2 打印文件的最后5行