f



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

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

0
James
6/13/2006 8:01:08 PM
comp.unix.shell 15484 articles. 3 followers. Post Follow

8 Replies
1442 Views

Similar Articles

[PageSpeed] 14

James wrote:
> 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?

How about learning "grep" first?
grep -vf FileB FileA
-- 
pgancarz, at, o2, pl
0
Pawel
6/13/2006 8:09:34 PM
On 13 Jun 2006 13:01:08 -0700, James 
  <jzheng22@gmail.com> wrote:
> 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?
>
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"
0
Bill
6/13/2006 8:23:47 PM
Pawel Gancarz wrote:
> James wrote:
> 
>>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?
> 
> 
> How about learning "grep" first?
> grep -vf FileB FileA

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
0
Michael
6/13/2006 8:31:04 PM
2006-06-13, 20:09(+00), Pawel Gancarz:
> James wrote:
>> 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?
>
> How about learning "grep" first?
> grep -vf FileB FileA

That doesn't answer the question.

grep -Fxvf fileB fileA

would have.

-- 
St�phane
0
Stephane
6/13/2006 9:45:03 PM
James <jzheng22@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 !!!
0
dfeustel
6/13/2006 10:30:04 PM
James wrote:
> 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

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

0
William
6/14/2006 2:01:30 AM
"James" <jzheng22@gmail.com> writes:
>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?

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)

0
John
6/17/2006 4:29:46 AM
Le Sat, 17 Jun 2006 04:29:46 +0000, John Savage a �crit�:

> "James" <jzheng22@gmail.com> writes:
>>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?
> 
> 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.

  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
Loki
6/25/2006 2:56:34 PM
Reply:

Similar Artilces:

new URL("c:/dir/file") OR new URL("c:\dir\file") OR new URL("file://c:/dir/file")
new URL("c:/dir/file") OR new URL("c:\dir\file") OR new URL("file://c:/dir/file") What is the correct syntax ? Is it the same as a new File ?FileDialog send: "c:\Program Files\dir\file". I have the feeling that different classes (File, URL, URl) have the different syntax. Please say a), b) or c) and answer me does it is same for all java classes which ask for a file path? FreeStyler =E3=81=AE=E3=83=A1=E3=83=83=E3=82=BB=E3=83=BC=E3=82=B8: > new URL("c:/dir/file") OR new URL("c:\dir\file") OR new > URL("file://c:/dir/fil...

CONVERTING a ".sas" file into ".dat" file
hello , i use the codes first to import from excel and second to convert from .xls to .sas format .But I need to have the data in .dat format.Kindly guide :- (1) PROC IMPORT OUT= WORK.MARCH DATAFILE= "c:\Sas\MARCH.xls" DBMS=EXCEL REPLACE; SHEET="march$"; GETNAMES=YES; MIXED=NO; SCANTEXT=YES; USEDATE=YES; SCANTIME=YES; RUN; (2) * convert the excel file into a SAS data file and put it into a directory "c:\Sas"* libname dis "c:\Sas"; proc import datafile="C:...

software "file.txt" to "file.dat"
Hi, I' d like to ask you if there is a way to transform a "file.txt" into a "file.dat" format. Thanks in advance Aurelio Malo ...

is there definition of "binary file" or "text file"
even so called "text file", acturally it's also stored in the binary format. niy38@hotmail.com (Niy) writes: > even so called "text file", acturally it's also stored in > the binary format. There isn't a difference on unix. Unix sees a file as simply a sequence of bytes, which it does not try to interpret. Interpretation of file formats is done at the application level. For example, a database program would expect a certain format, a text editor another format, but the standard I/O routines just deal with streams of bytes. What's the issue you...

How to convert a ".p" file to ".m" file
I got a algorithm package. But the matlab file is in p-file. How can I convert these file to m-file so that I can read the source code of the algorithm. Does anyone can help me thanks tinyloop wrote: > > > I got a algorithm package. But the matlab file is in p-file. How > can > I convert these file to m-file so that I can read the source code > of > the algorithm. > Does anyone can help me > thanks Cannot be done and should not be tried / per tinyloop <fcen@ee.cuhk.edu.hk> wrote: > I got a algorithm package. But the matlab file is in p-file. How > can...

suppressing << when using "matlab < file.m" from a unix shell
I have a file name file.m which has the following contents : A = 5 B = A * 5 C = 5 I get the following o/p when I run it at a unix shell prompt as : matlab -nodisplay -nosplash < file.m -bash-3.2$ matlab -nodisplay -nosplash < file.m < M A T L A B > Copyright 1984-2007 The MathWorks, Inc. Version 7.5.0.338 (R2007b) August 9, 2007 To get started, type one of these: helpwin, helpdesk, or demo. For product information, visit www.mathworks.com. >&...

How to use ".so" files without header files
Hi. Is anybody know how to use ".so" files without header files.? I need it to use Adobe plugin to view "pdf" files (nppdf.so). Opera and Mozilla use this file without nppdf header files. How does Mozilla know what functions are in "nppdf.so"? Do you know any links on this subject? Thanx in advance ebleble ebleble@poczta.fm (ebleble) writes: > Hi. > Is anybody know how to use ".so" files without header files.? I need > it to use Adobe plugin to view "pdf" files (nppdf.so). Opera and > Mozilla use this file withou...

File specification and "file" statement"
On Sun Sololaris, with sas 9.1.3, I want to save the content of dataset to a text file. Any idea why this does not work.? data null; set test3; file "/export/samfs-bcf/rcf-12/mkhan// Create_account_&uscid..&rundate..txt"; put col1; run; Getting follwing error messages: 23 ERROR 23-2: Invalid option name /. The SAS System 79 + file "/export/samfs-bcf/rcf-12/mkhan// Create_account_&uscid..&rundate..txt"; ------ 23 ERROR 23-2: Invalid option name...

Info about using File Exchange "files"
Hi, Being new to this community, I have a very simple question: In order to use these "Files" to which directory I should be copying? Thanks, Sandip Sandip Deshpande: > I have a very simple question: > In order to use these "Files" to which directory I should be copying? any folder that is in your ML-path - or the current folder - will do us Hi, Thanks a lot for the reply. But as I can understand the scope of these functions will be local to the folder. What if I want to use them as MATLAB function? Thanks, Sandip Sandip: <SNIP adding m-files to his/her syste...

What is a "File URL" as opposed to a "non-file URL"
Hi, Sorry to be thick, but what is a "File URL"? Looking on the web, the closest I got to it was a URL that literally starts with "File:"; is that correct? The reason I ask is that I'm looking at IE Java security settings in my IE browser and it says that "Connect to non-file url codebase" is OK but "File Based" is disabled. As I hope to have the flexibility to use a subset of FTP as the protocol to upload my Applets from VMS I am hoping that "File Based" does mean "file:" and does not pertain to FTP vs HTTP. Does it just mean &...

why construct file with "", the path of the file return "/"? 364127
l am a little puzzled, why the file's behavior like this: File fl = new File("","1.txt"); System.out.println(fl.getName()); System.out.println(fl.getParent()); System.out.println(fl.getPath()); System.out.println(fl.getAbsolutePath()); try { System.out.println(fl.getCanonicalPath()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } ...

Re: software "file.txt" to "file.dat"
On 1/1/05 at 2:33 AM, malo@mncn.csic.es (Aurelio F Malo) wrote: >I' d like to ask you if there is a way to transform a "file.txt" >into a "file.dat" format. I am certain there is. But I cannot provide details until you specify the format details of each file. For example, you could have "file.txt" formatted as: a single numeric record per line multiple mixed records per line with records separated by tabs, spaces, commas etc Mathematica Lists The point is neither the extension .txt nor the extension .dat are sufficient to specify the fo...

Re: CONVERTING a ".sas" file into ".dat" file
hi ... just one suggestion (addition) to the code put _all_; writes values preceded by the variable names, plus the SAS automatic variables data _null_; set sashelp.class (obs=2); put _all_; run; Name=Alfred Sex=M Age=14 Height=69 Weight=112.5 _ERROR_=0 _N_=1 Name=Alice Sex=F Age=13 Height=56.5 Weight=84 _ERROR_=0 _N_=2 make one addition ... data _null_; set sashelp.class (obs=2); put (_all_) (:); run; Alfred M 14.0 69.0 112.5 Alice F 13.0 56.5 84.0 and another idea ... if you want the data aligned in columns ... data _null_; set sashelp.class (obs=2); pu...

Problem with "Skip file types" in "Find Files".
I would like to search for text files based on some texts. And I want to exclude svn file (i.e. *.svn-base). So I add the extension .svn-base in "Skip file types" and expect Matlab "Find Files" GUI to skip the svn files. However, "Find Files" still returns the svn file. I think "Find Files" has problem with this kind of filename, eg. "abc.c.svn-base". Is it treating "c.svn-base" as extension? However, I try out the Matlab function FILEPARTS and it works fine. >> [pathstr, name, ext, versn] = fileparts('abc.c.svn-base') ...

why construct file with "", the path of the file return "/"? 204909
l am a little puzzled, why the file's behavior like this: File fl = new File("","1.txt"); System.out.println(fl.getName()); System.out.println(fl.getParent()); System.out.println(fl.getPath()); System.out.println(fl.getAbsolutePath()); try { System.out.println(fl.getCanonicalPath()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } the output is: 1.txt \ \1.txt D:\1.txt D:\1.txt why "" path, java process it as "/"? junzhang1983@gmail.com wrote: > l am a little puzzled, ...

How to redirect "out" and "o" files to another directory?
Hi folks, when i compile my code in tornado , i am getting "o" and "out" files ( say within "/PPC604gnu/"). I want to copy "out" files automatically to some other directory after compilation of my code. If possible where i have to change ? ..Raja In your Tornado project, under properties of the build, look for the MACRO called POST_BUILD_RULE. Put in a line to copy the files. The make file will run that line in the shell after the build completes. LC ...

&quot;Save measurement to file&quot;-VI doesn't obey the &quot;Use next avaible file name&quot; setting
I'm using this express VI to store data. In it's properties, there's the "If a file already exists: Use next avaible file name" radio box which does not work. Instead, it appends everything to the one file it created on the first run. My VI has a while-loop with an embedded flat sequence with the writing-VI being in the last frame of the flat sequence. LV 8.5, Linux Hi There. Could You make a screendump of Your VI? I tried using the Express VI on both LV7 Express (Base,Win), and LV8.5 (Base, Win) , and it works perfectly.... &nbsp; I've attached my VI. Current...

use "old" .so-files with "new" compiler?
Hi, I am quite sure this question has answered here already several times, but I haven't found the answer yet. Anyway, here is the question again: I've got libraries (*.so) that are built with gcc 2.95.2. The gcc version, I've installed on my computer, is 3.xx and I don't have the source files of the libraries. Can I link those libraries without uninstalling my new 3.xx-compiler? Thanx for your answers in advance. Isn't there a secret flag like "gcc -useoldlib"? *g* Philipp "Philipp" <capri.sonne@gmx.de> wrote... > Hi, I am quite sure this q...

File.new("file.ext", "w").write data
Consider... File.new("file.ext", "w").write data Is this safe? When will the file be closed? For IO it's said: "I/O streams are automatically closed when they are claimed by the garbage collector." Is this true for files as well, (File is a subclass of IO)? I know you can do... File.open("file.ext", "w"){|f| f.write data} ...., but the first one is shorter and better readable. gegroet, Erik V. Erik, The file handle will indeed stay open until the File instance is GC'd. The block-passing version is a few extra characters, but...

PSP7
How do I reset the default "Files of type" in "File Open" window to "All files" ? Wasn't it P who wrote: >How do I reset the default "Files of type" in "File Open" window to "All >files" ? Click on the little down arrow to open the list. "All Files" is the top entry in the list. -- Mike Williams Gentleman of Leisure "Mike Williams" <nospam@econym.demon.co.uk> wrote in message news:CHTTuBADDwMCFwyr@econym.demon.co.uk... > Wasn't it P who wrote: > >How do I reset the default "...

Fast alternatives to "File" and "IO" for large numbers of files ?
People, I have script that does: - statistical processing from data in 50x32x20 (32,000) large input files - writes a small text file (22 lines with one or more columns of numbers) for each input file - read all small files back in again for final processing. Profiling shows that IO is taking up more than 60% of the time - short of making fewer, larger files for the data (which is inconvenient for random viewing/ processing of individual results) are there other alternatives to using the "File" and "IO" classes that would be faster? Thanks, Phil. ...

How to convert a 2D "bmp" file to a 3D "3ds" file?
This video tutorial explains how to convert a 2D "bmp" file to a 3D "3ds" file using "WinTopo Freeware" and "Google SketchUp Pro". http://www.youtube.com/watch?v=mu7PcaiBq8I ...

PSP7
How do I reset the default "Files of type" in "File Open" window to "All files" ? Wasn't it P who wrote: >How do I reset the default "Files of type" in "File Open" window to "All >files" ? Click on the little down arrow to open the list. "All Files" is the top entry in the list. -- Mike Williams Gentleman of Leisure "Mike Williams" <nospam@econym.demon.co.uk> wrote in message news:CHTTuBADDwMCFwyr@econym.demon.co.uk... > Wasn't it P who wrote: > >How do I reset the default "...

can the command "dir" display the "file verison" of a file?
I would like to use DOS command to display the "file verison" of a file, and I have checked with the "help", it seems "dir" does not have this parameter. Can someone help me? Thanks a lot <mander.lala@gmail.com> wrote: >I would like to use DOS command to display the "file verison" of a > file, and I have checked with the "help", it seems "dir" does not have > this parameter. What "file version" is that? DOS-files doesn't have any such thing. If you mean the build-version in a PE/Win execu...

Web resources about - How to use "sed" to subtract a file from another file - comp.unix.shell

Resources last updated: 2/15/2016 11:25:42 PM