2

Hi I am writing a script in bash which read the contents of files that have the word "contact"(in the current directory) in them and sorts all the data in those files in alphabetical order and writes them to a file called "out.txt". I was wondering if there was any way in which I could get rid of duplicate content. Any help would be appreciated

The code I have written so far.

#!/bin/bash

cat $(ls | grep contact) > out.txt
sort out.txt -o out.txt
Grzegorz Piwowarek
  • 13,172
  • 8
  • 62
  • 93
Anmol Wadhwa
  • 329
  • 2
  • 3
  • 8
  • "How to get rid of duplicates? [duplicate]" :D ..that made my day. Answer: you search for an existing question before you post your question :D – Youda008 Nov 06 '15 at 11:41

4 Answers4

5

sort has option -u (long option: --unique) to output only unique lines:

sort -u out.txt -o out.txt

EDIT: (Thanks to tripleee)

Your script, at present, contains problems of parsing ls output,

This is a better substitute for what you are trying to do:

sort -u *contact* >out.txt
mohit
  • 5,696
  • 3
  • 24
  • 37
  • Or better yet `sort -u *contact* >out.txt` to get rid of the unsightly [parsing of `ls` output](http://mywiki.wooledge.org/ParsingLs) as well. – tripleee Aug 11 '13 at 15:40
4

Use this using the uniq command (easier to remember than flags)

#!/bin/bash

cat $(ls | grep contact) | sort | uniq > out.txt

or the -u flag for sort like this

#!/bin/bash

cat $(ls | grep contact) | sort -u > out.txt
woofmeow
  • 2,370
  • 16
  • 13
0

uniq may do what you need. It copies lines from input to output, omitting a line if it was the line it just output.

icktoofay
  • 126,289
  • 21
  • 250
  • 231
0

Take a look at the "uniq" command, and pipe it through there after sorting.