+ Reply to Thread
Results 1 to 9 of 9

Thread: Copying a Specific Line from a File

  1. #1
    garrensilverwing's Avatar
    garrensilverwing is offline x10 Sophmore garrensilverwing is an unknown quantity at this point
    Join Date
    Nov 2008
    Posts
    148

    Copying a Specific Line from a File

    Hi again guys!

    I have something I am failing at figuring out. I want to write some code to copy the entire line of a particular file so that I can move it to another file. However, I do not know how to copy the line of the file. I know exactly which line it will be 100% of the time I just can't find anything on google to help me.

    Thanks again!
    Last edited by garrensilverwing; 09-14-2009 at 10:59 PM.

  2. #2
    4xware is offline x10Hosting Member 4xware is an unknown quantity at this point
    Join Date
    Jun 2009
    Posts
    4

    Re: Copying a Specific Line from a File

    It depends on the programming language.
    For example, in PHP you can try the file() function.
    Last edited by 4xware; 09-15-2009 at 06:58 AM.

  3. #3
    siremethmimetes is offline x10Hosting Member siremethmimetes is an unknown quantity at this point
    Join Date
    Sep 2009
    Posts
    5

    Re: Copying a Specific Line from a File

    Greetings,

    It depends on where you are and what you can use. I know how to do it real easy with perl, but that might not be available to you. I am pretty sure the Shell can as well. But that would take skills beyond my current ken.

    With joy and peace in Christ,
    Sir Emeth Mimetes

  4. #4
    ah-blabla's Avatar
    ah-blabla is offline x10 Lieutenant ah-blabla is an unknown quantity at this point
    Join Date
    Sep 2009
    Posts
    375

    Re: Copying a Specific Line from a File

    You probably have to read through all the lines beforehand first before you can read the line you want. Alternatively you can use a random access file object (I know there's something like this for java), but they are completely unaware of lines, so you need to know from exactly which byte you want to read.

  5. #5
    descalzo's Avatar
    descalzo is offline Grim Squeaker descalzo has a brilliant futuredescalzo has a brilliant futuredescalzo has a brilliant future
    Join Date
    Jul 2009
    Location
    Ankh-Morpork
    Posts
    7,636

    Re: Copying a Specific Line from a File

    1. Which language? The two most applicable are PHP and Perl if you are talking about a web script.
    2. When you say you know 'which line' it is, is it by number or by some specific content?
    3. Do you want to just copy the line, or do you want to remove it from the original file?
    4. Do you want to just add the line to the destination file, overwriting what is there, or do you want to add it to the end, etc?
    Add:
    5. Local file or a web page?
    Last edited by descalzo; 09-15-2009 at 10:20 AM.
    Nothing is always absolutely so.

  6. #6
    garrensilverwing's Avatar
    garrensilverwing is offline x10 Sophmore garrensilverwing is an unknown quantity at this point
    Join Date
    Nov 2008
    Posts
    148

    Re: Copying a Specific Line from a File

    sorry i forgot to mention i'm using php, im not too versed with perl

  7. #7
    liketobemad is offline x10Hosting Member liketobemad is an unknown quantity at this point
    Join Date
    Sep 2009
    Posts
    26

    Re: Copying a Specific Line from a File

    For PHP
    it is very simple load contents into a array by file.. and append to the destination
    Have a look here
    http://www.w3schools.com/PHP/php_file.asp
    or go to
    http://php.net
    and have a look for file() function

  8. #8
    descalzo's Avatar
    descalzo is offline Grim Squeaker descalzo has a brilliant futuredescalzo has a brilliant futuredescalzo has a brilliant future
    Join Date
    Jul 2009
    Location
    Ankh-Morpork
    Posts
    7,636

    Re: Copying a Specific Line from a File

    PHP Code:
    $all_lines file"PATH_TO_THE_FILE_WITH_THE_LINE"); 
     
    # NOTE: NUMBERING STARTS AT 0, NOT 1
     
    $the_line $all_lines"NUMBER_OF_YOUR_LINE"];  
     
     
    # ERASES ANYTHING IN THE FILE AND JUST WRITES THE LINE 
     
    file_put_contents"PATH_TO_DESTINATION_FILE" $the_line  ); 
     
    # IF YOU WANT TO ADD IT TO THE END OF A FILE... 
     
    file_put_contents"PATH_TO_DESTINATION_FILE" $the_line FILE_APPEND ); 
    Last edited by descalzo; 09-15-2009 at 01:57 PM.
    Nothing is always absolutely so.

  9. #9
    garrensilverwing's Avatar
    garrensilverwing is offline x10 Sophmore garrensilverwing is an unknown quantity at this point
    Join Date
    Nov 2008
    Posts
    148

    Re: Copying a Specific Line from a File

    Ahhh thanks a lot descalzo, i got it to work how i wanted!

+ Reply to Thread

Similar Threads

  1. Perl Help
    By cgrim29588 in forum Free Hosting
    Replies: 1
    Last Post: 02-16-2009, 02:06 PM
  2. XHTML help please
    By mac173 in forum Graphics & Webdesign
    Replies: 3
    Last Post: 01-29-2008, 11:39 AM
  3. A little help plz
    By pineau in forum Free Hosting
    Replies: 3
    Last Post: 11-22-2007, 04:43 PM
  4. php version
    By loveispoison in forum Free Hosting
    Replies: 10
    Last Post: 11-21-2007, 10:53 AM
  5. November Desktop
    By n4tec in forum Off Topic
    Replies: 12
    Last Post: 11-08-2005, 07:18 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers