Reputation: 47
Trying to fix the output content with my script, but not work..
Let's name these two file as "File A" & "File B"
File A is content string like:
C Test aa.test.com
D Test bb.example.com
G Test cc.try.example.com
K Test dd.test.com
M Test cc.ee.try.example.com
O Test .test.com
T Test gg-1-2.example.com
U Test hh.example.com
X Test example.com
File B is:
test.com
example.com
try.example.com
Trying compare two files and to make output like:
C Test test.com
D Test example.com
G Test try.example.com
K Test test.com
M Test try.example.com
O Test test.com
T Test example.com
U Test example.com
X Test example.com
Here is my sample code:
#!/bin/bash
File_A="/root/temp/File_A"
File_B="/root/temp/File_B"
awk -v File_B="$File_B" -v OFS=" " '
BEGIN {
while ( ( getline < File_B ) > 0 ){
VAL = $0
sub( /^[^ ]+ /, "", VAL )
DICT[ $1 ] = VAL
}
}
{
print $0, DICT[ $1 ]
}' $File_A
exit
After output i still get the same content as File A, couldn't figure out.
C Test aa.test.com
D Test bb.example.com
G Test cc.try.example.com
K Test dd.test.com
M Test cc.ee.try.example.com
O Test .test.com
T Test gg-1-2.example.com
U Test hh.example.com
X Test example.com
or could be possible achieved from other command?
Upvotes: 2
Views: 153
Reputation: 41446
This awk
should do:
awk 'FNR==NR {arr[$0];next} {for (i in arr) {c=match($3,i);n=c&&(!b[$3]||c<b[$3])?i:n;b[$3]=c}$3=n}1' File-B File-A
C Test test.com
D Test example.com
G Test try.example.com
K Test test.com
M Test try.example.com
O Test test.com
T Test example.com
U Test example.com
X Test example.com
Upvotes: 1
Reputation: 850
first sort FILE_B
by line length and save it to FILE_C
:
cat FILE_B | awk '{ print length(), $0 }' | sort -nr | cut -d ' ' -f 2- > FILE_C
then run this command:
awk 'BEGIN{c=0;}FNR==NR{a[c++]=$0;next;} {for(i in a){if(match($3,a[i])){$3=a[i];print $0;next;} } }' FILE_C FILE_A
output:
C Test test.com
D Test example.com
G Test try.example.com
K Test test.com
M Test try.example.com
O Test test.com
T Test example.com
U Test example.com
X Test example.com
Upvotes: 2
Reputation: 5414
you can use this :
grep -of file_B file_A | paste <(grep -f file_B file_A | cut -d' ' -f1,2 ) -
Upvotes: 3