-1

I have literally been at this for 5 hours, I have busybox on my device, and I unfortunately do not have -X in grep to make my life easier.

edit;

I have two list both of them have mac addresses, essentially I am just wanting to achieve offline mac address lookup so I don't have to keep looking it up online

list.txt has vendor mac prefix of course this isn't the complete list but just for an example

00:13:46
00:15:E9
00:17:9A
00:19:5B
00:1B:11
00:1C:F0

scan will have list of different mac addresses unknown to which vendor they go to. Which will be full length mac addresses. when ever there is a match I want the line in scan to be output.

Pretty much it does that, but it outputs everything from the scan file, and then it will output matching one at the end, and causing duplicate. I tried sort -u, but it has no effect its as if there is two different output from two different methods, the reason why I say that is because it will instantly output scan file that has everything in it, and couple seconds later it will output the matching one.

From searching I came across this

#!/bin/bash


while read line; do

grep -F 'list' 'scan'  

done < list.txt

which displays the duplicate result when/if found, the output is pretty much echoing my scan file then displaying the matched pattern, this creating duplicate

This is frustrating me that I have not found a solution after click on all the links in google up to page 9.

Please someone help me.

andyADD
  • 610
  • 1
  • 6
  • 20
  • 1
    Can you explain what it is that you have and what you want? You could put it before the `From searching` section... – beroe Dec 11 '14 at 08:09
  • Please share a sample input of `list.txt` and also `scan` so we can get a better idea of what can be happening. – fedorqui Dec 11 '14 at 11:42
  • Ok I edit my OP, hopefully it will be easier to understand. – andyADD Dec 11 '14 at 12:55
  • What about `grep -F -f list.txt 'scan'` ? – Mark Setchell Dec 11 '14 at 13:10
  • Sorry I did not comment yesterday. if I use that command it will spit out mac address + "this is not a directory" – andyADD Dec 12 '14 at 13:12
  • Youe code does not make sense. Where does `dword` come from? Your problem description sounds roughly like what you would expect if this was simply an empty variable, though that doesn't explain why you get an additional line of output at the end. – tripleee Dec 13 '14 at 10:51
  • `-X` is not a common `grep` option. Do you mean `-x` or perhaps `-f`, which would suit your problem better? – tripleee Dec 13 '14 at 10:52
  • -x does not exist in the world of openwrt grep version. -f will spit out 00:11:22 (mac address) is not a directory – andyADD Dec 14 '14 at 11:39
  • @tripleee I edit up dword was variable using cut and other things I forgot to remove that. – andyADD Dec 15 '14 at 16:37

2 Answers2

0

I don't know if the Busybox sed supports this out of the box, but it should be easy to do in Awk or Perl instead then.

Create a sed script to print lines from file2 which are covered by a prefix in file1 by transforming each line in file1 into a sed command to print a match for that regular expression:

sed 's%.*%/&/p%' file1 | sed -n -f - file2

The same in Awk:

awk 'NR==FNR { a[++i]="^" $0; next }
    { for (j=1; j<=i; ++j) if ($0 ~ a[j]) print }' file1 file2
tripleee
  • 175,061
  • 34
  • 275
  • 318
  • Hey sorry for the late response, unfortunately sed gives me this error "sed: can't open '-': No such file or directory" I also removed - between -f and file2 but gives me - unsupported command. I also tried awk command, and it gave me the same output where it prints the whole list of mac address then at the end of the list it prints the duplicate it finds – andyADD Dec 15 '14 at 15:19
  • Try with a temporary file. But your symptoms sound like something else is wrong, too. Do you have DOS carriage returns in either of the input files? – tripleee Dec 16 '14 at 04:37
  • to be honest I am not sure what you mean by "carriage returns" – andyADD Dec 16 '14 at 07:06
  • It's what happens if you save a file on Windows and open it on Linux. This is an incredibly common and frustrating newbie problem. See if `dos2unix` is available, or google for alternatives. – tripleee Dec 16 '14 at 07:14
  • Sorry I had that problem long ago but none of my machines except one is running windows. Anyways I wrote the script directly on openwrt to prevent encoding problems. And if I happen to write any bash scripts on windows, sublime text provides encoding to linux which I have used and tested, and it has worked out. unfortunately that is not the problem. I think its just busybox being busybox. – andyADD Dec 17 '14 at 10:00
0

Ok guys I did a nested for loop (probably very in efficient) but I got it working printing the matching mac addresses using this

#!/usr/bin/bash

for scanlist in `cat scan | cut -d: -f1,2,3`
do

for listt in `cat list`
do

if [[ $scanlist == $listt ]]; then

grep $scanlist scan

fi

done
done

if anyone can make this more elegant but it works for me for now. I think the problem I had was one list contained just 00:11:22 while my other list contained 00:11:22:33:44:55 that is why I cut it on my scanlist to make same length as my other list. So this only output the matches instead of doing duplicate output.

andyADD
  • 610
  • 1
  • 6
  • 20