Home > Enterprise >  awk for string comparison with multiple delimiters
awk for string comparison with multiple delimiters

Time:06-24

I have a file with multiple delimiters, I m looking to compare the value after the first / when read from right left with another file.

code :-

awk -F'[/|]' NR==FNR{a[$3]; next} ($1 in a )' file1 file2 > output

cat file1

AAB/BBC/customer|fed|12931|
/customer|fed|982311|
BXC/DEF/OTTA|fed|92374|
AVD/customer|FST|8736481|
FFS/T6TT/BOSTON|money|18922|
GTS/trust/YYYY|opt|62376|
XXY/IJSH/trust|opt|62376|

cat file2

customer
trust

expected output :-

AAB/BBC/customer|fed|12931|
/customer|fed|12931|
AVD/customer|FST|8736481|
XXY/IJSH/trust|opt|62376|

CodePudding user response:

$ awk -F\| '            # just use one FS
NR==FNR {
    a[$1]
    next
}
{
    n=split($1,t,/\//)  # ... and use split to the 1st field
    if(t[n] in a)       # and compare the last split part
        print
}' file2 file1

Output:

AAB/BBC/customer|fed|12931|
/customer|fed|982311|
AVD/customer|FST|8736481|
XXY/IJSH/trust|opt|62376|
  • Related