Ok, lets suppose I have two files like so:
file1
John 5441223
Sandy 113446
Jill 489799
file2
Sandy Tuesday
Jill Friday
John Monday
Is it possible to match records from these two files and output them into one output file? For example, lets suppose I want to output like this:
John Monday 5441223
Sandy Tuesday 113446
etc...
Preferably by looping if possible so I don't have sloppy code.
No loop needed
$ cat file1
John 5441223
Sandy 113446
Jill 489799
$ cat file2
Sandy Tuesday
Jill Friday
John Monday
$
$
$ sort < file1 > file1.sorted
$ sort < file2 > file2.sorted
$
$ join file2.sorted file1.sorted
Jill Friday 489799
John Monday 5441223
Sandy Tuesday 113446
$
Thanks for the info. I figured this but any way to do with grep and looping because I'm using a very large data set. Thanks.
"grep and looping"? That would not be efficient. grep has to read the entire file. This is faster.
So it's possible, but less efficient? How would we do that anyway, say, with a loop like this:
for ******
do
******
done > loop.out
anbu23
6
while read nm day
do
grep $nm file1 | read name no
echo $name $day $no
done <file2
or
awk ' { arr[$1] = arr[$1] ? arr[$1]" "$2 : $2 }
END {
for( key in arr ) { print key " " arr[key] }
}' file2 file1