106
votes
Accepted
Common lines between two files
Use comm -12 file1 file2 to get common lines in both files.
You may also needs your file to be sorted to comm to work as expected.
comm -12 <(sort file1) <(sort file2)
From man comm:
-1 ...
9
votes
Accepted
Linux equivalent of windows cmd command
Taking a guess as to what those Windows commands do, I'd say the equivalent in a POSIX sh script would be:
equal=no
cmp -s file1 file2 && equal=yes
which would set the equal variable to yes ...
8
votes
Accepted
bash remove common lines from two files
As an alternative to comm, consider grep:
grep -vxFf /tmp/required /tmp/all
This asks for the lines in /tmp/all that do not (-v) exist in the file (-f) /tmp/required. To avoid interpreting any line ...
4
votes
Accepted
How do I find duplicate lines in multiple files within folders
You could do this (if no files have a tab caracter in their names):
grep -T -r . mainfolder | sort -k 2 | uniq -D -f 1
The recursive grep will output each line prefixed by the filename it is in. Then ...
4
votes
Compare two files based on first column
$ awk 'NR==FNR{a[$1]; next} FNR==1 || !($1 in a)' file2 file1
ID firstname lastname
4 Brenda Something
NR==FNR{a[$1]; next} Use first column from file2 to build array keys
...
4
votes
Accepted
Print only what is exclusive to a file compared to another in Bash
Not being able to presort the files isn’t a problem:
comm -13 <(sort fileA) <(sort fileB)
This gives
1199.com
1299.com
www2.1329.com
with your examples, assuming each host is on a separate ...
4
votes
Common lines between two files
Since you're running on Linux, I suppose it's GNU/Linux and you are using the GNU diff command.
If you're running the GNU diff command, this is how to see all changed lines as well as common lines:
...
4
votes
Accepted
Comparing Data between 2 different files in Unix
For comm to work properly, both files have to be sorted lexicographically, not numerically. You may sort your files before calling comm using
sort -o file1 file1
sort -o file2 file2
Then:
$ comm -...
3
votes
RFCOMM device seems to be missing (dev/rfcomm0)
Even if the bluetooth service was started, the /dev/rfcomm0 device would not just automatically appear.
At minimum, the bluetooth subsystem would need to know the address of the Bluetooth device and ...
3
votes
Linux equivalent of windows cmd command
Your windows command appears to compare two files with the fc command, and searches the output for a "no differences" message; if we see that message then set the variable equal to yes.
The unix ...
3
votes
diff and comm are not finding difference between two env files
You do not seem to be running diff and comm on any files, you are comparing the output of the env command.
Since you are comparing env, the sourcing of the environment files is not doing what you ...
2
votes
Linux equivalent of windows cmd command
In the bash shell, the && operator is a logical-and for process control, allowing a second process to be run if the first process didn't exit with errors. A double pipe (||) does similar, but ...
2
votes
Comparing Data between 2 different files in Unix
You can use grep
grep -F -x -f 'File2' -v 'File1'
Pattern of fixed--strings (-F) in File2 (-f)
Use -x to match whole lines. Otherwise "0123" in File1 would be excluded from the output due to "12" in ...
2
votes
Issues of using sort and comm
Just to flesh out Kevin's comprehensive and illustrative answer: if you run comm with the case-insensitive flag, comm -i, you must also sort case-insensitively, e.g., sort -f.
Full example:
comm -i &...
2
votes
Accepted
comm command behaving strangely
You are almost certainly right that additional characters on each line are causing corresponding lines to fail to match exactly. Those additional characters might have the form of carriage-return ...
2
votes
What do comm and diff try to accomplish at input/output level?
As noted here;
https://en.m.wikipedia.org/wiki/Diff
"The operation of diff is based on solving the longest common subsequence problem."
and as noted in the comments there are multiple ...
2
votes
How to find the intersection of multiple files (not necessarily two files)?
A function allows for a recursive approach
f() {
if (($# == 1))
then
cat $1;
return;
fi
comm -12 $1 <(f "${@:2}")
}
f file1 file2 file3 file4 file5...
2
votes
Compare two files and generate another on matching condition
$ awk -F'/' 'NR==FNR{b[$1]; next} {sub(/^zn/,"")} !($1 in b)' b.txt a.txt
4i8l
2
votes
Accepted
Group results using comm
My only guess at 3 is to loop over each line (skipping first) and if the next result has a different xxx= vs abc= then print a -------- but I am not in love with that.
Your guess is just fine. Do it ...
1
vote
Recursively list path of files only
Note that directories on Unix are just one of many types of files. With find, you can search for them with -type d, or use the / qualifier in zsh globs. Other types of files include regular files (-...
1
vote
How to get the difference between files
If one of the files is a DOS formatted text file while the other file is a Unix formatted text file, then each line will be different from all lines in the other file, even if the letter on the lines ...
1
vote
How to get well-formed table from comm?
You could do:
$ comm <(echo "1\n2") <(echo "2\n3") | awk -F'\t' -v OFS=, '{NF=3;print}'
1,,
,,2
,3,
Where awk forces the Number of Fields to be 3 and converts from an input ...
1
vote
get only the unmatched list as an output
You can do it with awk. The challenge in this case is that in active.txt the ports are immediately followed by a :, whereas in all.txt they stand isolated on the line. So you need to change the field ...
1
vote
Accepted
get only the unmatched list as an output
grep -v will work, just swap source with target. - will use pipe for -f
instead -x whole line -w whole word is recommended to ignore whitespaces from all.txt
cut -d\ -f1 active.txt | grep -vxFf - all....
1
vote
Compare two files and generate another on matching condition
The following bash script should do the job (since bash 4 or higher):
#!/bin/bash
readarray -t a_arr < a.txt
readarray -t b_arr < b.txt
for a_el in "${a_arr[@]}"
do
# remove the ...
1
vote
How to find the intersection of multiple files (not necessarily two files)?
No parallel nor xargs, nor comm necessary. Try a function
$ intersection() { sort $@ | uniq -c | sed -n "s/^ *$# //p"; }
$ intersection file[1-3]
line2
line4
1
vote
Accepted
How to find the intersection of multiple files (not necessarily two files)?
When you dereference RES in:
comm $FILE ${RES}
the content of RES replaces ${RES}. But comm expects a filename as argument, so for instance if $RES contains hello comm tries to open a file named ...
1
vote
What do comm and diff try to accomplish at input/output level?
The general problem for a diff implementation is to find the next
common block of text after it detected a deletion or an insertion.
In order to make the result useful, the implementation needs to ...
1
vote
Accepted
Printing in one line the common text using comm cmd?
The comm utility is used to compare whole lines between files. What you want to do is to join on a particular field.
$ join -t, file2 file1
number_123,hold,this car is under maintenance
number_345,...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
comm × 53diff × 18
text-processing × 14
bash × 9
grep × 7
shell × 6
awk × 6
files × 5
find × 5
sort × 5
shell-script × 4
file-comparison × 4
linux × 3
scripting × 2
join × 2
sed × 1
logs × 1
command × 1
raspbian × 1
csv × 1
bluetooth × 1
nginx × 1
text-formatting × 1
character-encoding × 1
text × 1