Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

For example: I have a CSV file (first file ) with below data (vertically):

1
2
3
4
5
6

Second CSV with below data (vertically):

5
6
7
8

I want my resultant file (action needs to be performed using Unix shell scripting) to show only values (vertically) in first file but not in second file:

1
2
3
4
share|improve this question
    
Does each file have only one column? Are both files sorted already? –  Mark Plotnick Jan 28 at 14:41
    
yes both files have only 1 column and we can sort using sort command –  user3116123 Jan 28 at 15:03

3 Answers 3

Assuming the files are in sorted order you can use the comm command to compare the 2 files and show the results that are in file1 but not in file2.

$ comm -23 file1 file2
1
2
3
4

The -23 switch tells comm to omit the lines that are only in file2 (-2) and the lines that are in both (-3).

share|improve this answer

Use the following command :

diff file1 file2 | grep '<' | cut -c3-

The diff takes the difference of the files file1 and file2 , then we grep '<' the output of the first command to get the lines that belong to the first file only. The output is then fed to cut -c3-to truncate the first two characters so that '<' and ' ' are eliminated.

share|improve this answer

Pass one file as a pattern to grep:

grep -vwFf 2.csv 1.csv 
1
2
3
4

From man grep:

   -f FILE, --file=FILE
          Obtain  patterns  from  FILE,  one  per  line.   The  empty file
          contains zero patterns, and therefore matches nothing.   (-f  is
          specified by POSIX.)
   -w, --word-regexp
          Select  only  those  lines  containing  matches  that form whole
          words.  The test is that the matching substring must  either  be
          at  the  beginning  of  the  line,  or  preceded  by  a non-word
          constituent character.  Similarly, it must be either at the  end
          of  the  line  or  followed by a non-word constituent character.
          Word-constituent  characters  are  letters,  digits,   and   the
          underscore.
   -F, --fixed-strings
          Interpret PATTERN as a  list  of  fixed  strings,  separated  by
          newlines,  any  of  which is to be matched.  (-F is specified by
          POSIX.)
   -v, --invert-match
          Invert the sense of matching, to select non-matching lines.  (-v
          is specified by POSIX.)
share|improve this answer
    
Oh baby you're in the final stretch for 20k now 8-) –  slm Jan 28 at 21:05
    
@slm sweet! Thanks :) –  terdon Jan 28 at 21:06

Your Answer

 
discard

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.