Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8615059
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T05:16:44+00:00 2026-06-12T05:16:44+00:00

I have a test file: 1 2 3 4 5 6 7 8 This

  • 0

I have a test file:

1
2
3
4
5
6
7
8

This command prints the last 4 lines of the file:

$ awk 'BEGIN{"wc -l < file" | getline b}{if(NR>b-4) print}' file
5 
6 
7 
8 
userpc@userpc-desktop

Now I want to do the same, but the command system ():

$ awk '{if( NR > (system("wc -l < file")-4) ) print}' file
8 
1 
8 
2 
8 
3 
8 
4 
8 
5 
8 
6 
8 
7 
8 
8 
userpc@userpc-desktop:

How to improve the last command system ()? I also want to print 4 last lines of the file.
Thank you for your help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T05:16:45+00:00Added an answer on June 12, 2026 at 5:16 am

    awk is definitely not the right tool for this. It’s common to do: sed '1{N;N;N;}; N; D; $p', and you can do something similar with awk:

    awk '{for( i=0;i<3;i++) a[i]=a[i+1];a[3]=$0} END {print a[0],a[1],a[2],a[3]}' OFS='\n'
    

    Basically, you keep track of the last four lines seen, and print them all when you get to the end of the file. You can be a little more obscure and efficient with:

    awk '{a[++i%4]=$0} END {print a[++i%4],a[++i%4],a[++i%4],a[++i%4]}' OFS='\n'
    

    but, really, why would you?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have one file (for example: test.txt), this file contains some lines and for
I have these (sample) lines in a HTML-file: test.ABC.test test.ABCD.test test.ABCE.test And this Ant
I have a haskell file test.hs. In this file I have written a function
I have this test code that just saves an XML file to a folder.
I have a file, which I read from test . This file is UTF-8.
I have an Application Object. In Global.asax file i use this: Application[obj] = test;
I have this inside my spring xml file: <object type=Test.Web.Utilities.TestClass, Test.Web singleton=false id=TestClass> <property
I have one file /tmp/test.txt this is test that is test When I run
I have a CSV-file similar to this test.csv file: Header 1; Header 2; Header
Say I have this file: cat > test.txt <<EOF line one word line two

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.