Score:0

How can I diff two files, deleting lines in file1 from file2

jp flag

I want to take file1 and remove all the lines that are contained in file2, ideally keeping the filename file1 once done, keeping the unmodified lines of anything remaining.

I've tried using diff as follows

diff --suppress-common-lines file1 file2 > newfile

This gives me either and output < or > at the start of each line depending on the order of the two files, which I don't want.

Also tried using comm:

comm -23 file1 file2 > newfile

this requires line to be re-ordered, and the lines I have are all order sensitive.

Any idea how I can achieve this?

hr flag
It *sounds* like you want something like `grep -vxFf file2 file1` or `awk 'NR==FNR{a[$0]; next} !($0 in a)' file2 file1` - see for example [Comparing two text files and save the missing](https://askubuntu.com/questions/1153850/comparing-two-text-files-and-save-the-missing)
th flag
@steeldriver make that an answer
jp flag
Thanks for the suggestions. This work for the most part, the files are an comparing are network switch configurations, which use the ! character between sections. Both these solutions remove these characters, and I would like to keep them. is there a way to keep those characters?
hr flag
@wavers please [edit] your question with a minimal example of the 2 files and your desired output. Or delete this question and ask a more specific one - as it stands, it's a duplicate of the one I linked.
jp flag
Never mind, my base fil included the !, took those out and all is good. Thanks
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.