Copying a Specific Line from a File

garrensilverwing

New Member
Messages
148
Reaction score
0
Points
0
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:

4xware

New Member
Messages
4
Reaction score
0
Points
0
It depends on the programming language.
For example, in PHP you can try the file() function.
 
Last edited:

siremethmimetes

New Member
Messages
5
Reaction score
0
Points
0
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
 

ah-blabla

New Member
Messages
375
Reaction score
7
Points
0
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.
 

descalzo

Grim Squeaker
Community Support
Messages
9,373
Reaction score
326
Points
83
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:

descalzo

Grim Squeaker
Community Support
Messages
9,373
Reaction score
326
Points
83
PHP:
$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:
Top