简体   繁体   中英

Bash: compare two files and change the output

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?

您可以使用此:

grep -of file_B file_A | paste <(grep -f file_B file_A | cut -d' ' -f1,2 ) -

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

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

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM