I want to compare file1 with file2 and generate a file3 which contains the lines in file1 which are not present in file2.
相关问题
- How to get the return code of a shell script in lu
- Invoking Mirth Connect CLI with Powershell script
- Why should we check WIFEXITED after wait in order
- Emacs shell: save commit message
- “command not found” errors in expect script execut
相关文章
- 使用2台跳板机的情况下如何使用scp传文件
- In IntelliJ IDEA, how can I create a key binding t
- shell中反引号 `` 赋值变量问题
- How get the time in milliseconds in FreeBSD?
- Making new files automatically executable?
- Reverse four length of letters with sed in unix
- Launch interactive SSH bash session from PHP
- Generate disk usage graphs/charts with CLI only to
Try
It ususally works much better in most cases for me. You may want to sort files prior, if order of lines is not important (e.g. some text config files).
For example,
If you need to solve this with coreutils the accepted answer is good:
You can also use sd (stream diff), which doesn't require sorting nor process substitution and supports infinite streams, like so:
Probably not that much of a benefit on this example, but still consider it; in some cases you won't be able to use
comm
norgrep -F
nordiff
.Here's a blogpost I wrote about diffing streams on the terminal, which introduces sd.
Consider this:
file a.txt:
file b.txt:
You can find the difference with:
The output will be:
You can redirict the output in an output file (c.txt) using:
This will answer your question:
The Unix utility
diff
is meant for exactly this purpose.See the manual and the Internet for options, different output formats, etc.
Many answers already, but none of them perfect IMHO. Thanatos' answer leaves some extra characters per line and Sorpigal's answer requires the files to be sorted or pre-sorted, which may not be adequate in all circumstances.
I think the best way of getting the lines that are different and nothing else (no extra chars, no re-ordering) is a combination of
diff
,grep
, andawk
(or similar).If the lines do not contain any "<", a short one-liner can be:
but that will remove every instance of "< " (less than, space) from the lines, which is not always OK (e.g. source code). The safest option is to use awk:
This one-liner diffs both files, then filters out the ed-style output of diff, then removes the trailing "<" that diff adds. This works even if the lines contains some "<" themselves.
Use the Diff utility and extract only the lines starting with < in the output