Find the answer to your Linux question:
Results 1 to 5 of 5
Greetings list. So, I have a file ("linefile") with ~600 line numbers in one column, e.g. 92 1433 1725 1821 10265 etc. I want to extract the lines with these ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Sep 2008
    Location
    Copenhagen
    Posts
    5

    Extract specific lines from a file using a list of line numbers from another file


    Greetings list.

    So, I have a file ("linefile") with ~600 line numbers in one column, e.g.

    92
    1433
    1725
    1821
    10265

    etc.

    I want to extract the lines with these numbers from another file ("infile").

    I think awk should be able to do the job, but can't figure out how. Any suggestions?

    Cheers,
    BBAF

  2. #2
    Trusted Penguin Irithori's Avatar
    Join Date
    May 2009
    Location
    Munich
    Posts
    3,392
    yes, you are right. awk, (e)grep come to mind.

    However, some more info is needed about the structure of that infile.
    for example:
    - do these numbers occur at the beginning of a line, when they occur?
    - or do they occur in a certain row?
    - do they occur before or after a certain char, i.e. "," or ":" ?

    based on information like these, a RegularExpression can be created.
    Right now, it's too vague.

    Well ok, one can egrep for lines with numbers in it (like you wrote).
    This would look like this:
    egrep [0-9] infile

    If there is at least one number in the line, it will match.
    Regardless, where this number is or how often there is a number.

    This is almost certain too much, I guess?

  3. #3
    Linux User
    Join Date
    May 2008
    Location
    NYC, moved from KS & MO
    Posts
    251
    How about
    Code:
    for line in `cat linefile`; do sed -n "${line}p" infile; done

  4. #4
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    use grep with -f option
    Code:
    grep -f file1 file2

  5. #5
    Just Joined!
    Join Date
    Sep 2008
    Location
    Copenhagen
    Posts
    5

    [solved]

    Thanks all for your replies.
    Secondmouse, your suggestion solved the problem

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •