Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a file containing my personal details (.txt). How can I via the terminal copy only a few lines in the file and bring it to a new .txt file?

ok, for eg, this is my details.

name : farah age : 23 phone number : 0123 education : degree

How can i copy only age and phone number and bring those to a new.txt?

share|improve this question
It depends on what you want copied to the new file. Do you want the first few lines, the last few, lines containing a string, not containing a string, lines between two strings? There are tools to do all these, but we need to know what you wish to do. – Arcege Aug 31 '11 at 17:04

There are several ways to do this. If your file has some known structure, you can use grep. The grep command searches a file for a specific phrase and returns lines that match that phrase. So if your file looks like

Name: Sally

Date of Birth: 7.31.76

Address: 1234 Main St.

SSN: 123-45-6789

you can run grep Name info.txt and it will return Name: Sally. You can then redirect the output to another file. So calling

grep Name info.txt > info2.txt

will output the line to the new file info2.txt. If you want to append new lines, you can do

grep Address info.txt >> info2.txt

otherwise the file will be overwritten.

You could also learn to use a command line text editor like vim.

share|improve this answer

You can use grep to search for a regular expression in details.txt and redirect the result to the new file.

If all the lines you want to copy have something in common the other lines don't you can use :

grep "string in common" details.txt > new.txt

If not you will have to search for each line you want to copy, still using grep, and append them to new.txt using >> in stead of >.

share|improve this answer

There are also editors which work in the terminal, e.g. nano, vi and emacs.

If you are using a graphical user interface on your local machine and a terminal on a remote machine, you can also use the mouse to copy and paste from one terminal window/tab to a second one.

share|improve this answer

Assuming input file details.txt contains:

name: farah
age: 23
phone number: 0123
education: degree

you can select lines "name" and "phone" by extended grep and redirect output to new.txt:

grep -E "age:|phone number:" details.txt > new.txt

This will produce new.txt with:

age: 23
phone number: 0123

How it works:

Grep prints only matched lines. The -E options enabled extended regexp which gives you possibility to use | (alternative). Remember to quote whole pattern, so | will be interpreted by grep. Otherwise shell will try to interpret. You don't want this here.

share|improve this answer
ok thanks for the help. – MsWanie Sep 4 '11 at 14:18
@MsWanie: if it's helpful, you should vote it up and accept (the most helpful one). – Michał Šrajer Sep 4 '11 at 15:43

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.