Find the answer to your Linux question:
Results 1 to 9 of 9
Hi, I have a file (lines.txt) and each line of this file contains a number. I want to copy these number of lines from a txt file and create a ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Apr 2012
    Posts
    58

    Copy multiple lines using sed


    Hi,

    I have a file (lines.txt) and each line of this file contains a number. I want to copy these number of lines from a txt file and create a new file.
    For example:

    The lines.txt is like:
    1
    3
    4

    The text file that I have is:
    aaaaa
    aaa
    bbbbbbb
    ccc
    dddd

    and the result should be:
    aaaaa
    bbbbbbb
    ccc

    Could you please help me?

    Thanks in advance!

  2. #2
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    Posts
    11,662
    Is this a class exercise?
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  3. #3
    Just Joined!
    Join Date
    Apr 2012
    Posts
    58
    hi,

    no I need it for personal use.

  4. $spacer_open
    $spacer_close
  5. #4
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    Posts
    11,662
    Ok. Forums use policies don't let us do your homework for you...

    I would use a bash script with an embedded sed command to extract the data you need from the second file. It would read 1 line at a time from the file with the numbers in it, and then use either sed to output and concatenate the output from the second file into the output file. I think using sed by itself would either be really difficult, or impossible for what you want.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  6. #5
    Just Joined!
    Join Date
    Apr 2012
    Posts
    58
    hi again,

    Yes this is exactly what i need. I tried:

    Code:
    sed -n '1,3p' infile > outfile
    but I cannot do it with the lines.txt

    Thanks again for your time.

    Cheers,

  7. #6
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    Posts
    11,662
    So, as an exercise for you, develop the shell code to get each line at a time into a variable called vline. Then the sed part of this, assuming that the second input file is file2.txt, would go like this:
    Code:
    # while not end of file, read line into linedat variable
    vline="${linedat}p"
    sed -n $vline file2.txt >>output.txt
    So, vline would contain the strings "1p", "3p" and "4p" which would make the sed command print the 1st, 3rd, and 4th lines of file2.txt into the output file.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  8. #7
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    Posts
    11,662
    FWIW, the bash man page has everything you need to figure this out! Just me doing a little "teaching how to fish" here...

    Also, I have tested this out, and it works!
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  9. #8
    Just Joined!
    Join Date
    Apr 2012
    Posts
    58
    Thank you!!! Now I can understand how it works with files!

  10. #9
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    Posts
    11,662
    Quote Originally Posted by corfuitl View Post
    Thank you!!! Now I can understand how it works with files!
    The -n option to sed is critical here as it keeps it from printing all the other lines as well. IE 1,3p will, without -n, show all the lines, but with the first 3 doubled up. Remember, man pages are your friend!
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

Posting Permissions

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