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 8448941
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T10:33:41+00:00 2026-06-10T10:33:41+00:00

I have two files, one file is my data, and the other file is

  • 0

I have two files, one file is my data, and the other file is a list of line numbers that I want to extract from my data file. Can I use awk to read in my lines file, and then extract the lines that match the line numbers?

Example:
Data file:

This is the first line of my data
This is the second line of my data
This is the third line of my data
This is the fourth line of my data
This is the fifth line of my data

Line numbers file

1
4
5

Output:

This is the first line of my data
This is the fourth line of my data
This is the fifth line of my data

I’ve only ever used command line awk and sed for really simple stuff. This is way beyond me and I have been googling for an hour without an answer.

  • 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-10T10:33:43+00:00Added an answer on June 10, 2026 at 10:33 am

    One way with sed:

    sed 's/$/p/' linesfile | sed -n -f - datafile
    

    You can use the same trick with awk:

    sed 's/^/NR==/' linesfile | awk -f - datafile
    

    Edit – Huge files alternative

    With regards to huge number of lines it is not prudent to keep whole files in memory. The solution in that case can be to sort the numbers-file and read one line at a time. The following has been tested with GNU awk:

    extract.awk

    BEGIN {
      getline n < linesfile
      if(length(ERRNO)) {
        print "Unable to open linesfile '" linesfile "': " ERRNO > "/dev/stderr"
        exit
      }
    }
    
    NR == n { 
      print
      if(!(getline n < linesfile)) {
        if(length(ERRNO))
          print "Unable to open linesfile '" linesfile "': " ERRNO > "/dev/stderr"
        exit
      }
    }
    

    Run it like this:

    awk -v linesfile=$linesfile -f extract.awk infile
    

    Testing:

    echo "2
    4
    7
    8
    10
    13" | awk -v linesfile=/dev/stdin -f extract.awk <(paste <(seq 50e3) <(seq 50e3 | tac))
    

    Output:

    2   49999
    4   49997
    7   49994
    8   49993
    10  49991
    13  49988
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created two JavaScript files. One file is "validators.js" and the other is
Say I have two files where there is one number per line File 1
I have two php files, one manages database connection and the other retrieves data
I have two different modules that need access to a single file (One will
I have two project, one which generates some artifacts from a WSDL file using
I have a file named file with three lines: line one line two line
I have two files. file1 has the data like belowing containing only one column.
I have two tables, one is a list of songs. The other is a
I have a code comprised of two functions one that reads data and the
I have two files, one containing an array in PHP that is echo ed

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.