1

How can I copy same specific parts of a file to another file? In the first file, I have:

user01: Id name nationality
user02: Id name nationality
.
.
.

I want to copy each piece of information to a specific file (one file for Id and another for name and another one for nationality).

3
  • 1
    Use cat infront i.e. cat file1.txt > file2.txt
    – heemayl
    Commented Mar 25, 2015 at 10:34
  • 2
    Please formulate your question in a way so that it's clear what you want. It is often very helpful to amend the question with clear data samples, so that we can unambiguously see what you have in the various files and how to compose that data to obtain what you expect as result. Keep the data samples as terse as possible, but as expressive as necessary.
    – Janis
    Commented Mar 25, 2015 at 11:14
  • 2
    Do you want a separate file for each user? So, 3 files per user? Or do you want all ids in one file, all names in another etc? Please edit your question and show us your desired output.
    – terdon
    Commented Mar 25, 2015 at 12:44

4 Answers 4

2

cp is usually used for copying.

cp file1.txt file2.txt

To append to an existing file, use

cat file1.txt >> file2.txt
1
  • Unfortunately, the OP completely changed their question.
    – terdon
    Commented Mar 25, 2015 at 12:43
2

The command for that is cp:

cp file1.txt file2.txt

If you want to append to another existing file you can use:

cat file1.txt >> file2.txt
3
  • thank you it's good but if i want to copy a specific part or lines how i do Commented Mar 25, 2015 at 10:40
  • 1
    @AomineDaiki can you edit your question to explain more about 'specific part' ? line with a word ? without whitespace ? line 10 and above ?
    – Archemar
    Commented Mar 25, 2015 at 10:42
  • Unfortunately, the OP completely changed their question.
    – terdon
    Commented Mar 25, 2015 at 12:43
0

You can do it like:

sed 'h;s/\(: *[^ ]*\)\(.*\)/\1/pw id
     g;s//:\2/;h;s//\1/pw name
     g;s//:\2/w nationality' <infile

... which will write all output to stdout, but only the relevant stuff to each file. Running that on your example data, and I get:

user01: Id
user01: name
user01: nationality
user02: Id
user02: name
user02: nationality

... printed to stdout, and ...

head id name nationality

==> id <==
user01: Id
user02: Id
==> name <==
user01: name
user02: name
==> nationality <==
user01: nationality
user02: nationality
-1

You can use the cut command. For example to extract the first column and save in a file you can type:

cut -f1 -d" " sample > file1.txt

the -f1 parameter specify which column do you want use, in this case the first one.

the -d" " is the separator

1
  • Hi and welcome to the site! I'm afraid this isn't an answer. Please edit your answer and show how cut can be used for what the OP requests. You're just giving the manual, not the needed command. You should also show how the output will be redirected to different files.
    – terdon
    Commented Mar 25, 2015 at 12:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .