Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to use "sed" to subtract a file from another file

2 views
Skip to first unread message

James

unread,
Jun 13, 2006, 4:01:08 PM6/13/06
to
Hi all,

Two text files: FileA and FileB: each line only contains a userid
(no spaces or any other special chars). FileA is a superset of FileB.
I want to remove those lines, which appears in FileB, from FileA.
I could write a couple of lines to do it, but I really want to learn
"sed",
so any sed guru could give a one-line sed to solve this?

Many thanks,

James

Pawel Gancarz

unread,
Jun 13, 2006, 4:09:34 PM6/13/06
to

How about learning "grep" first?
grep -vf FileB FileA
--
pgancarz, at, o2, pl

Michael Tosch

unread,
Jun 13, 2006, 4:31:04 PM6/13/06
to

Yes, grep is most easy here.

It is almost impossible with sed.
sed stands for stream editor - its input is a stream or pipe.
It cannot determin which filename it currently works on,
nor it can read commands from a pipe.

If both files are sorted, comm is another solution:

sort -o FileA FileA
sort -o FileB FileB

comm -13 FileB FileA

--
Michael Tosch @ hp : com

Bill Marcum

unread,
Jun 13, 2006, 4:23:47 PM6/13/06
to
On 13 Jun 2006 13:01:08 -0700, James
You asked for sed, but here is a simple one-line solution:
grep -vf FileB FileA


--
petribar:
Any sun-bleached prehistoric candy that has been sitting in
the window of a vending machine too long.
-- Rich Hall, "Sniglets"

Stephane CHAZELAS

unread,
Jun 13, 2006, 5:45:03 PM6/13/06
to
2006-06-13, 20:09(+00), Pawel Gancarz:

That doesn't answer the question.

grep -Fxvf fileB fileA

would have.

--
Stéphane

dfeu...@mindspring.com

unread,
Jun 13, 2006, 6:30:04 PM6/13/06
to
James <jzhe...@gmail.com> wrote:
> I really want to learn "sed",

You will get an excellent return on time spent if you
Read the O'Reilly book _sed & awk_ by Dale Dougherty.
--
Using OpenBSD with or without X & KDE?
See Dave's OpenBSD | X | KDE corner at
http://dfeustel.home.mindspring.com !!!

William James

unread,
Jun 13, 2006, 10:01:30 PM6/13/06
to

You say you want to subtract one file from another. Convert both files
to arrays and then use the "-" operator.

ruby -e 'puts gets(nil).to_a - gets(nil).to_a' FileA FileB

John Savage

unread,
Jun 17, 2006, 12:29:46 AM6/17/06
to

You'll get a strong hint on how to accomplish this in sed here:
http://
groups.google.com/group/comp.unix.questions/msg/7444d3fb518c70a3?hl=en&

As a one-liner? Hmmm, it's going to be one awfully long line.

The problem this method has with sed are metacharacters, e.g., if your
data contains a 5-character line such as "x\y\z" the method I illustrate
will endeavour to delete a 3-character line matching /^xyz$/, and so on.
Pre-processing the entire data file is the only way to get around this.
--
John Savage (my news address is not valid for email)

Loki Harfagr

unread,
Jun 25, 2006, 10:56:34 AM6/25/06
to

Using your (nice) hint I'd propose this form, quite short
but is it a sed one liner as it uses a pipe ?-) :

$ sed 's/^.*$/\/&\/d/' FileB | sed -f - FileA

Sample test :

$ seq 15 >MISCFILES/listA
$ cat MISCFILES/listB
3
7
10
12

$ sed 's/^.*$/\/&\/d/' MISCFILES/listB | sed -f - MISCFILES/listA
1
2
4
5
6
8
9
11
14
15

0 new messages