How to do the opposite of diff? [duplicate] - linux

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
how to show lines in common (reverse diff)?
Is there a command to do the opposite of diff? I want to compare two files if the same thing exists in both create a list of them. i am trying to figure out what entry's exist in both files.

Here is a solution that WILL NOT change the order of the lines:
fgrep -x -f file1 file2

Use the join command:
join a.txt b.txt
assuming the files are sorted; if not:
sort a.txt > sorted_a.txt; sort b.txt > sorted_b.txt; join sorted_a.txt sorted_b.txt

Related

Shell script make lines in one huge file into two seperate files in one go? [duplicate]

This question already has answers here:
How to save both matching and non-matching from grep
(3 answers)
Closed 1 year ago.
Currently My shell script iterate the lines in one huge file two times:
(What I want to do is just like the shell script below.)
grep 'some_text' huge_file.txt > lines_contains_a.txt
grep -v 'some_text' huge_file.txt > lines_not_contains_a.txt
but it is slow.
How to do the same thing only iterate the lines once?
Thanks!
With GNU awk:
awk '/some_text/ { print >> "lines_contains_a.txt" }
!/some_text/ { print >> "lines_not_contains_a.txt" }' huge_file.txt
With sed:
sed -n '/some_text/ w lines_contains_a.txt
/some_text/! w lines_not_contains_a.txt' huge_file.txt

how do i sort this list of emails using bash? [duplicate]

This question already has an answer here:
Output from sort does not appear to be sorted
(1 answer)
Closed 2 years ago.
I'm trying to sort a list of emails using a bash script with code below but I'm getting the wrong output, advice
sort -t # -k1,1
You will need to add -d for dictionary order and so:
sort -r -d -t# -k1,1

"Recursive hexdump" from command line, input and output with same name [duplicate]

This question already has answers here:
Looping over pairs of values in bash [duplicate]
(6 answers)
Closed 5 years ago.
I have some files in a directory "documents" (file1, file2, ...) and I would like to save them in another directory "documents_hex" with hexdump from command line. There is a way to use hexdump for each file in "documents" and save them in "documents_hex" ("documents_hex" is inside "documents") with the same name in input and output?
Example: file1 to /documents_hex/file1, file2 to /documents_hex/file2, ...
Check this code :
for file in `ls documents`
do
hexdump -x $file > documents_hex/$file
done

How to sort file names by specific part in linux? [duplicate]

This question already has answers here:
How can I sort file names by version numbers?
(7 answers)
Closed 8 years ago.
I have lots of files in my build folder and I am trying to sort them by using sort command.
The structure of the files are like that:
name - version - 'v' - build date
xxx-2.0.0-SNAPSHOT-V2014-07-10_18-01-05.log
xxx-2.0.0-SNAPSHOT-V2014-07-10_18-02-05.log
xxx-2.0.0-SNAPSHOT-V2014-07-10_18-03-05.log
xxx-2.0.0-SNAPSHOT-V2014-07-10_18-04-05.log
xxx-2.0.0-SNAPSHOT-V2014-07-10_18-05-05.log
if we assume that version string will be stay in 3 digit, sorting them is easy. What if I add different versions like 2.1 or 2.0.0.2 here ? I need a result like this:
xxx-2.1-SNAPSHOT-V2014-07-10_18-05-05.log
xxx-2.0.2-SNAPSHOT-V2014-07-10_18-04-05.log
xxx-2.0.0.2-SNAPSHOT-V2014-07-10_18-03-05.log
xxx-2.0.0.1-SNAPSHOT-V2014-07-10_18-02-05.log
xxx-2.0.-SNAPSHOT-V2014-07-10_18-01-05.log
$ cat file
xxx-2.0.2-SNAPSHOT-V2014-07-10_18-04-05.log
xxx-2.0.0.2-SNAPSHOT-V2014-07-10_18-03-05.log
xxx-2.1-SNAPSHOT-V2014-07-10_18-05-05.log
xxx-2.0.0.1-SNAPSHOT-V2014-07-10_18-02-05.log
xxx-2.0.-SNAPSHOT-V2014-07-10_18-01-05.log
$ sort -V -r -t- -k2,2 < file
xxx-2.1-SNAPSHOT-V2014-07-10_18-05-05.log
xxx-2.0.2-SNAPSHOT-V2014-07-10_18-04-05.log
xxx-2.0.0.2-SNAPSHOT-V2014-07-10_18-03-05.log
xxx-2.0.0.1-SNAPSHOT-V2014-07-10_18-02-05.log
xxx-2.0.-SNAPSHOT-V2014-07-10_18-01-05.log
Note: Some implementations of sort do not support -V option...
Explanation:
-V : Version sort
-t- : Split into columns with delimiter '-'
-k2,2: Sort by field 2 & only 2
-r : reverse sort (based on your expected output. Remove this flag, if not required.)

How to avoid the display of the 2 first line from a linux command output? [duplicate]

This question already has answers here:
What's the opposite of head? I want all but the first N lines of a file
(9 answers)
Closed 8 years ago.
I have a program that displays many line in the output
How I can make it display the all output except the first 2 lines?
easily using tail command:
tail -n+3
You could use awk
awk 'NR>2' file
In order to complete the triplet,
sed '1,2d' file

Resources