-1

I have two files

tmp1.txt

aaa.bbbbb.txt
bbb.aaaaa.txt
ddd.bbbbbb.txt
mmm.cccccc.txt

tmp2.txt

aaa first
bbb second
ccc third
ddd fourth
eee fifth
fff sixth

I want to compare these two files tmp1.txt and tmp2.txt such that first it use anything before the first"." i.e aaa,bbb,ccc,ddd and search that in tmp2.txt and if it finds the match displays as

aaa.bbbbb.txt first
bbb.aaaaa.txt second
ddd.bbbbbb.txt fourth

Thanks

user3784040
  • 111
  • 1
  • 1
  • 10
  • anything that you already tried yourself? – Erik Sep 17 '14 at 19:59
  • 4
    Especially after you've [posted this](http://stackoverflow.com/questions/25894094/compare-two-files-in-bash) you can show an attempted script of your own. – anubhava Sep 17 '14 at 20:02

1 Answers1

3

Using awk:

awk 'NR==FNR{a[$1]=$2; next}$1 in a{print $0,a[$1]}' tmp2.txt FS=\. tmp1.txt
aaa.bbbbb.txt first
bbb.aaaaa.txt second
ddd.bbbbbb.txt fourth

Use field separator FS at the end to have its effect on the file that follows it.

Tom Fenech
  • 72,334
  • 12
  • 107
  • 141
jaypal singh
  • 74,723
  • 23
  • 102
  • 147
  • 1
    +1 I was about to post something very similar but setting `FS` between the files is something new – Tom Fenech Sep 17 '14 at 20:08
  • 1
    @TomFenech Thanks for the edit. Here is a [reference](https://www.gnu.org/software/gawk/manual/html_node/Other-Arguments.html#Other-Arguments), but I'll be honest, I didn't know it either until I saw [Ed Morton](http://stackoverflow.com/a/24517482/970195) use it in one of his answer. – jaypal singh Sep 17 '14 at 20:16
  • 1
    Why am I not surprised that Ed had something to do with it :) – Tom Fenech Sep 17 '14 at 20:20