Main Page | Report this Page
 
   
Linux Forum Index  »  General Linux Discussion  »  how would one list all lines in one file not in...
Page 1 of 1    
Author Message
jameshanley39...
Posted: Thu Jul 24, 2008 9:12 am
Guest
hi

with what command/commands and how would one list all lines in one
file not in another file?

note- this would be useful were one comparing files in one directory
and not another

btw- are there any command to compare 2 directories.. list files in
one and not the other (I could do that from each directory to list
files not shared by both).

(I refer to files with full relative file path with filename, and not
the file contents itself, and not matching a file in one position of a
directory to a file of the same filename elsewhere in the other
directory. ).

thanks
jameshanley39...
Posted: Thu Jul 24, 2008 10:16 am
Guest
On Jul 24, 8:52 pm, noi ance <n... at (no spam) siam.com> wrote:
Quote:
On Thu, 24 Jul 2008 12:12:09 -0700, jameshanley39 typed this message:





hi

with what command/commands and how would one list all lines in one file
not in another file?

note- this would be useful were one comparing files in one directory and
not another

btw- are there any command to compare 2 directories.. list files in one
and not the other (I could do that from each directory to list files not
shared by both).

(I refer to files with full relative file path with filename, and not
the file contents itself, and not matching a file in one position of a
directory to a file of the same filename elsewhere in the other
directory.  ).

thanks

You could start with diff

$ man diff
$ info diff

$ diff -y  /folder1  /folder2

$ diff --help-


thanks.. I see

$ diff -q -r folder1 folder2
works really nicely to compare the filenames in the 2 directories, and
it says when file contents differs which is a nice bonus, too.
jameshanley39...
Posted: Thu Jul 24, 2008 10:20 am
Guest
On Jul 24, 8:43 pm, Stefan Patric <n... at (no spam) thisaddress.com> wrote:
Quote:
On Thu, 24 Jul 2008 12:12:09 -0700, jameshanley39 wrote:
hi

with what command/commands and how would one list all lines in one file
not in another file?

note- this would be useful were one comparing files in one directory and
not another

btw- are there any command to compare 2 directories.. list files in one
and not the other (I could do that from each directory to list files not
shared by both).

(I refer to files with full relative file path with filename, and not
the file contents itself, and not matching a file in one position of a
directory to a file of the same filename elsewhere in the other
directory.  ).

The problem (or asset, depends on how you look at it) with Linux/Unix is
the plethora of commands to do almost anything and find that one or few
that want you need to be done.  Start your search with these basic
commands:  diff, find, grep, egrep, fgrep, and gawk.  Familiarize
yourself on the use of "the pipe" symbol '|' with which you can "pipe"
the output from one command to another.  You should also study shell
scripting and a particular shell's built-in commands to automate your
searches and save typing, if you intend to do this a lot.

Stef-

I am a bit familiar with pipes and basic use of shell scripts and
grep.

People give grep a pattern and a file or data to find the pattern in,
but how do you tell grep to take each line of a file as its pattern to
look for ?

It looks like I might do something like that to list all lines in one
file and not another, wouldn't know how though.

thanks
Stefan Patric...
Posted: Thu Jul 24, 2008 2:43 pm
Guest
On Thu, 24 Jul 2008 12:12:09 -0700, jameshanley39 wrote:

Quote:
hi

with what command/commands and how would one list all lines in one file
not in another file?

note- this would be useful were one comparing files in one directory and
not another

btw- are there any command to compare 2 directories.. list files in one
and not the other (I could do that from each directory to list files not
shared by both).

(I refer to files with full relative file path with filename, and not
the file contents itself, and not matching a file in one position of a
directory to a file of the same filename elsewhere in the other
directory. ).

The problem (or asset, depends on how you look at it) with Linux/Unix is
the plethora of commands to do almost anything and find that one or few
that want you need to be done. Start your search with these basic
commands: diff, find, grep, egrep, fgrep, and gawk. Familiarize
yourself on the use of "the pipe" symbol '|' with which you can "pipe"
the output from one command to another. You should also study shell
scripting and a particular shell's built-in commands to automate your
searches and save typing, if you intend to do this a lot.

Stef
noi ance...
Posted: Thu Jul 24, 2008 2:52 pm
Guest
On Thu, 24 Jul 2008 12:12:09 -0700, jameshanley39 typed this message:

Quote:
hi

with what command/commands and how would one list all lines in one file
not in another file?

note- this would be useful were one comparing files in one directory and
not another

btw- are there any command to compare 2 directories.. list files in one
and not the other (I could do that from each directory to list files not
shared by both).

(I refer to files with full relative file path with filename, and not
the file contents itself, and not matching a file in one position of a
directory to a file of the same filename elsewhere in the other
directory. ).

thanks

You could start with diff

$ man diff
$ info diff

$ diff -y /folder1 /folder2

$ diff --help
Chris Burkhardt...
Posted: Tue Aug 26, 2008 11:12 am
Guest
jameshanley39 wrote:
Quote:
hi

with what command/commands and how would one list all lines in one
file not in another file?

If you just want to see the unique lines from the two files:

cat file1 file2 | uniq

- Chris
noi ance...
Posted: Tue Aug 26, 2008 4:40 pm
Guest
On Tue, 26 Aug 2008 10:12:26 -0600, Chris Burkhardt typed this message:

Quote:
jameshanley39 wrote:
hi

with what command/commands and how would one list all lines in one file
not in another file?

If you just want to see the unique lines from the two files:

cat file1 file2 | uniq

- Chris

Yes but you wouldn't know which file contained which line or the sequence
of lines.

diff --suppress-common-lines -y file1 file2
HASM...
Posted: Sun Aug 31, 2008 4:51 pm
Guest
noi ance <noi at (no spam) siam.com> writes:

Quote:
with what command/commands and how would one list all lines in one file
not in another file?

If you just want to see the unique lines from the two files:
cat file1 file2 | uniq

Yes but you wouldn't know which file contained which line or the sequence
of lines.
diff --suppress-common-lines -y file1 file2

I don't believe either of those solutions will work in the general case.

Uniq only detects identical adjacent lines, and cat'ing them together won't
do the job. Diff has the same problem, where only lines in corresponding
identical sections will be detected.

-- HASM
noi ance...
Posted: Sun Aug 31, 2008 11:08 pm
Guest
On Sun, 31 Aug 2008 14:51:01 -0700, HASM typed this message:

Quote:
noi ance <noi at (no spam) siam.com> writes:

with what command/commands and how would one list all lines in one
file not in another file?

If you just want to see the unique lines from the two files: cat file1
file2 | uniq

Yes but you wouldn't know which file contained which line or the
sequence of lines.
diff --suppress-common-lines -y file1 file2

I don't believe either of those solutions will work in the general case.

Uniq only detects identical adjacent lines, and cat'ing them together
won't do the job. Diff has the same problem, where only lines in
corresponding identical sections will be detected.

-- HASM

Not so the --suppress-common-lines hides lines occurring in both files,
while -y prints the diff in side by side display.

Isn't that what the OP wanted?

file1 file2
---- xyz text
abc text ---
-- ---
-- efg text
Chris Burkhardt...
Posted: Mon Sep 01, 2008 12:34 pm
Guest
HASM wrote:
Quote:
noi ance <noi at (no spam) siam.com> writes:

with what command/commands and how would one list all lines in one file
not in another file?

If you just want to see the unique lines from the two files:
cat file1 file2 | uniq

Yes but you wouldn't know which file contained which line or the sequence
of lines.
diff --suppress-common-lines -y file1 file2

I don't believe either of those solutions will work in the general case.

Uniq only detects identical adjacent lines, and cat'ing them together won't
do the job.

Oops, you are right. You'd have to throw a sort in there:

cat file1 file2 | sort | uniq

But you still wouldn't know which file contained which line (or in which order
the lines were originally).

- Chris
HASM...
Posted: Sat Sep 06, 2008 2:07 pm
Guest
noi ance <noi at (no spam) siam.com> writes:

Quote:
Not so the --suppress-common-lines hides lines occurring in both files,
while -y prints the diff in side by side display.

Isn't that what the OP wanted?

Not sure what the OP wanted, but the question was:

with what command/commands and how would one list all lines in one
file not in another file?

If I have two identical files, but with the lines in reverse order, e.g.:

% cat file1
1
2
3
% cat file2
3
2
1

The answer to the OP question should be an empty set, i.e. there isn't a
single line in file1 that isn't in file2. Running your diff command
yields:

% diff --suppress-common-lines -y file1 file2

1 <
2 <
Quote:
2
1

only picking up one of the lines as common.

-- HASM
HASM...
Posted: Sat Sep 06, 2008 2:12 pm
Guest
Chris Burkhardt <chris at (no spam) mretc.net> writes:

Quote:
with what command/commands and how would one list all lines in one file
not in another file?

cat file1 file2 | sort | uniq

But you still wouldn't know which file
contained which line (or in which order the lines were originally).

Yup. Not sure it can be done easily only with shell commands. That's why
we have perl.

-- HASM
noi ance...
Posted: Sat Sep 06, 2008 3:11 pm
Guest
On Sat, 06 Sep 2008 12:07:52 -0700, HASM typed this message:

Quote:
noi ance <noi at (no spam) siam.com> writes:

Not so the --suppress-common-lines hides lines occurring in both files,
while -y prints the diff in side by side display.

Isn't that what the OP wanted?

Not sure what the OP wanted, but the question was:

with what command/commands and how would one list all lines in one
file not in another file?

If I have two identical files, but with the lines in reverse order,
e.g.:

% cat file1
1
2
3
% cat file2
3
2
1

The answer to the OP question should be an empty set, i.e. there isn't a
single line in file1 that isn't in file2. Running your diff command
yields:

% diff --suppress-common-lines -y file1 file2

1
2
2
1

only picking up one of the lines as common.

-- HASM

One could compare apples to oranges to find the single common DNA strand
or more to the point, a binary file to a text file.

I made an assumption of file1.txt to file1.txt~ anything more extreme
than that warrants detailed analysis of both source files. For instance
your example could be solved by sorting the files before comparing.

Using the OP's requirements I think it fits the purpose of diff, ie,
compare 2 files line by line. Diff also handles binary compares if
needed.

I would only write a Perl, C++, Javascript program for repetitive
comparing of files that the diff could not handle. The OP did not
indicate any special circumstances.
Chris F.A. Johnson...
Posted: Sun Sep 07, 2008 2:00 am
Guest
On 2008-09-06, HASM wrote:
Quote:
noi ance <noi at (no spam) siam.com> writes:

Not so the --suppress-common-lines hides lines occurring in both files,
while -y prints the diff in side by side display.

Isn't that what the OP wanted?

Not sure what the OP wanted, but the question was:

with what command/commands and how would one list all lines in one
file not in another file?

If I have two identical files, but with the lines in reverse order, e.g.:

% cat file1
1
2
3
% cat file2
3
2
1

The answer to the OP question should be an empty set, i.e. there isn't a
single line in file1 that isn't in file2. Running your diff command
yields:

% diff --suppress-common-lines -y file1 file2

1
2
2
1

only picking up one of the lines as common.

diff --suppress-common-lines -y <(sort file1) <(sort file2)

--
Chris F.A. Johnson, webmaster <http://Woodbine-Gerrard.com>
===================================================================
Author:
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
pk...
Posted: Sun Sep 07, 2008 4:54 am
Guest
On Saturday 6 September 2008 21:12, HASM wrote:

Quote:
Chris Burkhardt <chris at (no spam) mretc.net> writes:

with what command/commands and how would one list all lines in one file
not in another file?

cat file1 file2 | sort | uniq

But you still wouldn't know which file
contained which line (or in which order the lines were originally).

Yup. Not sure it can be done easily only with shell commands. That's why
we have perl.

awk 'NR==FNR{a[$0];next} !($0 in a)' file1 file2

is the standard awk idiom to list all the lines in file2 that are not in
file1, regardless of the order in which they appear in the two files.
 
Page 1 of 1       All times are GMT - 5 Hours
The time now is Mon Dec 01, 2008 9:05 pm