Find the answer to your Linux question:
Results 1 to 4 of 4
I have a small script that loops through a list of words in one file and compares them to a second list of words. If the words in list1 also ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Aug 2009
    Posts
    2

    compare two lists without grep?


    I have a small script that loops through a list of words in one file and compares them to a second list of words. If the words in list1 also exist in list2 output those words to list3.

    for i in `cat ./list1` ; do
    if grep -wq "$i" ./list2 ; then
    echo "$i" >> ./list3
    fi
    done

    The problem is I am stuck with busybox and this grep doesn't have -w so it does a partial match. I need some way to compare whole words only.

  2. #2
    Trusted Penguin Irithori's Avatar
    Join Date
    May 2009
    Location
    Munich
    Posts
    3,382
    This is actually a line by line compare, so not exactly what you are asking.

    But if your busybox is compiled with comm,
    you might try this:
    Code:
    comm -1 -2 file1 file2 > file3
    BusyBox - The Swiss Army Knife of Embedded Linux

  3. #3
    Trusted Penguin Irithori's Avatar
    Join Date
    May 2009
    Location
    Munich
    Posts
    3,382
    sry, comm needs sorted lists.
    So, busybox would have to include: sort, uniq, comm
    Code:
    sort file1 | uniq > tmp_file1
    sort file2 | uniq > tmp_file2
    comm -1 -2 tmp_file1 tmp_file2 > file3
    rm tmp_file1 tmp_file2

  4. #4
    Just Joined!
    Join Date
    Aug 2009
    Posts
    2
    Don't have those either. I came up with this and it seems to work. I haven't tested every combination but . . .

    Thanks

    for i in `cat ./list1` ;do
    awk -v var=$i -F, '$1==var' ./list2 >> ./list3
    done

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •