使用awk脚本打印文本文件的最后n行

时间:2016-04-03 19:31:28

标签: awk

我需要一个脚本来打印文本文件的最后n行。文本文件名和行数可以改变,我想只调用脚本来打印任何文本文件的最后n行。我知道在第n行我可以使用NR< N;打印,但我怎么能在最后n行做,因为行数可以改变.-谢谢

5 个答案:

答案 0 :(得分:2)

有一个用于此目的的unix工具,称为[HttpGet] public ActionResult LogIn() { Viewbag.ReturnURL = Request.UrlReferrer.ToString(); return View(); } [HttpPost] public ActionResult LogIn(string token, string returnURL) { using (Model model = new Model()) if (model.Users.Any(_ => _.Token == token)) return Redirect(returnURL); return View(); } 。要获取最后100行文件,可以使用tail,然后直接使用输出或将其传递给其他程序,如awk。

答案 1 :(得分:1)

作为练习,还有另一个版本的交易空间与时间来实现相同的

$ awk -v n=10 'NR==FNR{a=NR;next} FNR>(a-n)' file{,}

首先扫描文件以获取行数,并用于第二次过滤最后n行。

答案 2 :(得分:1)

    private void GetScore() {
    AsyncHttpClient client = new AsyncHttpClient();
    RequestParams params = new RequestParams();
    params.put("email", textEmail);
    client.get("http://techcube.pk/game/getScore.php", params, new `enter code here`AsyncHttpResponseHandler() {

            @Override
            public void onSuccess(String s) {
                super.onSuccess("Success");
                String  score = null;
                try
                {
                    Log.d("Success", s);
                    JSONObject json = new JSONObject(s);
                     score = json.getString("Score");


                }


                catch (Exception e)
                {
                    e.printStackTrace();
                }
                //Toast.makeText(getApplicationContext(), score, Toast.LENGTH_LONG).show();
                fetchlivescore.setText(score);
            }
            @Override
            public void onFailure(Throwable throwable) {
                super.onFailure(throwable);
                Toast.makeText(getApplicationContext(), "Server Response for onFailure ", Toast.LENGTH_LONG).show();
            }
        });

}

设置$ cat file 1 2 3 4 $ cat tst.awk { rec[NR % n] = $0 } END { for (i=NR+1+(n<NR?0:n-NR); i<=(NR+n); i++) { print rec[i % n] } } $ awk -v n=2 -f tst.awk file 3 4 的起始值时的复杂性是为了适应您要求打印的记录多于文件中存在的记录的情况,例如:

i

答案 3 :(得分:0)

要在awk中本地执行此操作,您必须记住所看到的行:

awk -v n=10 '
    {line[NR]=$0}
    END {for (i=NR-(n-1); i<=NR; i++) print line[i]}
' file

为了节省内存,我们不需要记住整个文件;使用

    {line[NR]=$0; if (NR>n) delete line[NR-n]}

然而,反转文件更简单,打印第一行 n行,并重新反转输出

tac file | awk -v n=10 'NR <= n' | tac

但是使用tail要简单得多

答案 4 :(得分:0)

尝试使用此脚本:

{
  lines[(i=(++i%n))]=$0;
}
END {
  if (NR>=n) {
    linessize=n;
  } else {
    linessize=NR;
    i=0;
  }
  for(j=1;j<=linessize;j++) {
    print lines[(i+j)%n];
  }
}

文件只被解析一次。

使用 n 元素 的数组来缓冲读取的行。

测试:

$ printf "one line\n2nd line\n" | ./tail-awk.awk -f script.awk -v n=10
one line
2nd line
$ ./tail-awk.awk  -f script.awk -v n=10 <(man bash)
       attempted.  When a process is stopped, the shell immediately executes the next command in the sequence.  It  suffices  to
       place the sequence of commands between parentheses to force it into a subshell, which may be stopped as a unit.

       Array variables may not (yet) be exported.

       There may be only one active coprocess at a time.



GNU Bash-4.1                                            2009 December 29                                                 BASH(1)
$ ./tail-awk.awk -f script.awk -v n=5 /etc/apt/sources.list

deb http://archive.debian.org/debian-archive hamm main

deb ftp://ftp.debian.org/debian squeeze contrib

$
相关问题