-5

i have two files(a.txt , b.txt)

a.txt

boy
girl
grade
test_1
test_2

b.txt

boy
girl
grade
test_3
test_4

now i want to merge this two files(c.txt)

c.txt

boy
girl
grade
test_1
test_2
test_3
test_4

friends please help me! terminal command

3
  • 1
    The question is: is the first section of a.txt always b.txt? (i.o.w. is b an extended version af a)? Dec 7, 2015 at 10:54
  • 1
    You'll notice that answerers are having to guess what you mean. Please try to write clearer questions.
    – TRiG
    Dec 7, 2015 at 11:55
  • As mentioned, the question simply is unclear; is the section after grade possibly overlapping, does sorting matter, etc etc. please take the effort to make a proper question, guessing is not what we do. Dec 7, 2015 at 13:45

2 Answers 2

2
cat a.txt b.txt | sort | uniq > c.txt

Note that, this can work for more than 2 files, in general, the following with concatenate the contents of file1, file2, ....., fileN into outputFile :

cat file1 file2 file2 file4 .... fileN | sort | uniq > outputFile 
4
  • I think you should leave away the -c switch, as this will count the number of occurrences of each line.
    – s3lph
    Dec 7, 2015 at 11:53
  • Yes, well sported @the_Seppi . I have since removed, thanks. Dec 7, 2015 at 11:57
  • There is no need for a sort | uniq. sort -u should be enough.
    – A.B.
    Dec 11, 2015 at 20:07
  • And why the cat commands?
    – A.B.
    Dec 11, 2015 at 20:08
0

Assuming that the order of the lines doesn't have to be preserved (in this specific case the order of the lines will be preserved, but it may well not happen in other cases, since the files will be merged into a single file, the single file will be sorted and its non-unique lines will be discarded), using sort:

sort -u a.txt b.txt >c.txt
  • -u: prints only unique lines.
% cat a.txt
boy
girl
grade
test_1
test_2
% cat b.txt
boy
girl
grade
test_3
test_4
% sort -u a.txt b.txt >c.txt
% cat c.txt
boy
girl
grade
test_1
test_2
test_3
test_4

You must log in to answer this question.

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