Question:
LINUX UBUNTU : How to copy line in a file and paste it into a new file?
Wanie
2011-08-31 08:52:00 UTC
I have a file containing my personal details (.txt). How can I copy ONLY few lines in the file and bring it to a NEW .txt file? KINDLY ASSIST ME, PLEASE AND THANK YOU

p/s : Doing all this in terminal
Four answers:
BurrintheSaddle
2011-08-31 09:00:12 UTC
are you connected with SSH or in the GNOME terminal?



either way open two terminal screens, vi the target and source files in each separate screen





in the source file if using SSH to connection you can highlight and COPY from Source.



in the TARGET go to line in which you want to insert, do a ESC "o' Lower case O to open a new line.

then "paste the content" of the clipboard from the source. with ESC p



OR



if you wish to place a specific line, you can also grep for the line in SOURCE and concat it at the end of target.



grep "String" source



to see if it pulls the string you want.

to append the "String" at the end of the target file

simply



grep "String" source >> Target





I do a lot of preprocessing of files between database on a linux box, there are a lot of ways to play it but a simple edit is using vi. get familiar with it and becomes a very good friend. A lot of NOOBs struggle with it, but it isn't as complicated
Solution X
2011-08-31 09:03:20 UTC
simple open txt file that contains the details now select the lines which you want to copy then press ctrl+c or right click on selected text and click on copy now minimize the txt file

now open new txt file

and press ctrl+v or right click paste
† Oh yeah
2011-08-31 12:58:09 UTC
A few lines on the top? Use the head command.



A few lines on the bottom? Use the tail command.





This example will copy the first 3 lines on the top from file.txt and paste the output into newfile.txt



head -n3 file.txt > newfile.txt
ratter_of_the_shire
2011-08-31 15:08:14 UTC
AWK is a very powerful tool



http://www.ibm.com/developerworks/linux/library/l-awk1/index.html


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...