0

I have two files A.txt and B.txt and it contains some lines of names.

A.txt

AS
DF
GH

B.txt

SS
WE
DF
TR

I want to delete content of A if it matches with content of B and want to save the new file in A only so i did something like this

grep -Fvxf B A 

so i got output

AS
GH

But here i am unable to save the output in A only...please help.Thanks.

Inian
  • 80,270
  • 14
  • 142
  • 161
manas
  • 479
  • 2
  • 14
  • Is the ordering of your files important? It would be much easier to do this efficiently (which is to say, in a small constant amount of RAM and O(n+m) time) if they were guaranteed to be sorted. – Charles Duffy Jul 09 '21 at 20:10
  • without ordering i want to do...sir – manas Jul 09 '21 at 20:11
  • ...for the details of _how_, see [BashFAQ #36](http://mywiki.wooledge.org/BashFAQ/036) – Charles Duffy Jul 09 '21 at 20:12
  • It's not clear to me what you mean by "without ordering". Is it okay if the output files _come out_ in sort order, instead of preserving the original order? (Mind, being able to accept files in originally-unsorted order means you lose a lot of the performance/efficiency advantages that can be gained by requiring inputs to be presorted -- though that only matters much if your real inputs are much bigger than the example here). – Charles Duffy Jul 09 '21 at 20:12
  • See [How to find set difference of two files?](https://stackoverflow.com/questions/10489473/how-to-find-set-difference-of-two-files) – Charles Duffy Jul 09 '21 at 20:14
  • There is some problem in my processing if i sort the file...thats why i need the solution without sorting files....my aim is to auto replace the content of A after grepping...i mean i donot want to save the output to a new file – manas Jul 09 '21 at 20:15
  • To be clear, you _always_ save output to a new file. Even `sed -i` saves output to a new file, it just renames that file over the input file when it's done. There's no meaningful semantic difference between using `sed -i` and its equivalents and using `mktemp` and `mv` to write to a temporary file and rename it yourself. – Charles Duffy Jul 09 '21 at 20:18
  • ok sir i want like this only... – manas Jul 09 '21 at 20:18
  • Anyhow -- if you can't tolerate reordering, I believe we have existing answers describing how to get that with either `grep` or `awk` (both of which are suitable tools). – Charles Duffy Jul 09 '21 at 20:19
  • Sir if you want to suggest the solution please suggest... – manas Jul 09 '21 at 20:22
  • Using [Deleting lines from one file which are in another file](https://stackoverflow.com/questions/4780203/deleting-lines-from-one-file-which-are-in-another-file) in combination with [How can I use a file in a command and redirect output to the same file without truncating it?](https://stackoverflow.com/questions/6696842/how-can-i-use-a-file-in-a-command-and-redirect-output-to-the-same-file-without-t) gets you to where you're trying to be. – Charles Duffy Jul 09 '21 at 20:25
  • I closed the question because it's already answered, so we don't need another copy in our knowledge base. If you have a new, specific problem when trying to combine the linked duplicates, feel free to [edit] to make your attempt and the new issue clear. – Charles Duffy Jul 10 '21 at 13:54

0 Answers0