2

I have 2 files file1 which contains

1,2,3,4,5

file2 contains

4,5,6,7,8

output should be in such a way that new file assume it as file3 should contain

1,2,3,4,5,6,7,8,

and not only that if contents in file1 and file2 changes like

file1 new contents

10,11,12,13,14

file2 new contents

13,14,15,16,17,18

after merging file3 should should contain below values

1,2,3,4,5,6,7,8,10,11,12,13,14,15,16,17,18

I have tried several commands like sort, uniq, cat etc but it haven't worked

1
  • 1
    touch file3; cat file1 file2 file3 | sort | uniq ? You will need to translate the commas to newlines. sed or tr will do that for you. Commented Jul 30, 2014 at 9:18

2 Answers 2

2

Commands like sort and uniq work on lines.

All you have to do is convert commas to newlines, do sort -u or uniq and then convert the newlines back to commas, e.g.

$ cat a
1,2,3,4,5
$ cat b
4,5,6,7,8
$ cat a b | tr ',' '\n' | sort -u | tr '\n' ','
1,2,3,4,5,6,7,8, 

You may find Set Operations in the Unix Shell helpful.

Sign up to request clarification or add additional context in comments.

2 Comments

Hi, Thanq for your reply but I am getting errors the output is not in proper format
@VenkatKrishna I gave you an idea and an example. Try using what you know to adapt it to your needs.
2

If you want merge result to file3:

cat file1 file2 | sed s/,/\\n/g | sort  -u | tr "\\n" "," >> file3

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.