For example, `file1.txt, contains only variables:
a
b
d
c
file2.txt
contains <
, variable and path:
< a /x/y/1.c
< c /x/d/7.h
< f /x/c/6.s
< b /x/v/8.mk
< t /x/1.h
< d /x/5.so
file1.txt
variables should be compared with file2.txt
. If a match is found, the complete variable and path should be printed from file2.txt
.
Expected output:
< a /x/y/1.c
< b /x/v/8.mk
< d /x/5.so
< c /x/d/7.h
I tried with comm -3 file1.txt file2.txt
, but it is not effectively working.
diff
can't be used in my case.
if the lines in file2.txt not start with <, you can use this;
join <(sort file1.txt) <(sort file2.txt)
if starts with < char, you can use this;
join -11 -22 <(sort file1.txt) <(sort file2.txt)
Eg;
user@host:/tmp$ join -11 -22 <(sort file1.txt) <(sort file2.txt)
a < /x/y/1.c
b < /x/v/8.mk
c < /x/d/7.h
d < /x/5.so
-11 means file1.txt and first column, -22 means file2.txt and second column (after "<" char)
Solution for when the keys are not anywhere in the path: Try this
cat file2.txt | grep -f file1.txt
Switch the file names as need.
You asked for shell but I would answer via python
import sys
with open(sys.argv[1], "r") as f:
d = dict((e.rstrip().split()[1], e) for e in f)
with open(sys.argv[2], "r") as f:
for e in f:
e = e.rstrip()
if e in d:
print(d[e], end="")
If you saved it to run.py
, you can call it such that
python3 run.py file2.txt file1.txt > output.txt
It also preserves the order of file1.txt
compared to sorting solutions but loads second file into memory.
AWK is good for these things:
$ awk 'NR == FNR {keys[$0] = 1; next}keys[$2]' file1.txt file2.txt
< a /x/y/1.c
< c /x/d/7.h
< b /x/v/8.mk
< d /x/5.so
Basically file1.txt is buffered in the keys
array. And then each line from file2 is printed if column 2 is in said array:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.