Find the answer to your Linux question:
Results 1 to 6 of 6
I have a shell script that looks through a folder of files, by searching from certain keywords that I have stored in a txtfile, using grep. Is there a way ...
  1. #1
    Just Joined!
    Join Date
    Jul 2007
    Posts
    14

    shell script to file

    I have a shell script that looks through a folder of files, by searching from certain keywords that I have stored in a txtfile, using grep. Is there a way to list if I find more than one, have the output of both on the same line?

    Code:
    currentdir=$PWD
    echo $currentdir
    ls
    echo "Pick a file"
    read INPUT
    cd $INPUT
    pwd                                                                                 
    for filename in *
    do
       for searchterm in $(cat /home/searchterms.txt)                                                                                 
         do
           grep -q $searchterm $filename && echo -e "$filename : $searchterm \n" >>/home/burkhojt/results.txt
                                                                                    
         done
      echo
    done
    I want the output to look like this
    1089-006.doc : packet, radio

    instead of

    1089-006.doc : packet

    1089-006.doc : radio

  2. #2
    Linux User
    Join Date
    Jun 2007
    Posts
    318
    I'm not near my Linux box right now so I can't test it but this should work.

    Code:
    for filename in *
    do
       tmp=""
       for searchterm in $(cat /home/searchterms.txt)
         do
           grep -q $searchterm $filename && tmp="${tmp}, $searchterm"
         done
         if [ "$tmp" != "" ]
            then echo -e "$filename : $tmp \n" >>/home/burkhojt/results.txt
            fi
      echo
    done
    The output will end up looking like this:
    1089-006.doc : , packet, radio

    Use something like awk's substr function to remove that unwanted 1st comma.

    Vic

  3. #3
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    I prefer this:
    Code:
    for filename in *; do
        terms=
        first=1
        exec 3< /home/searchterms.txt
        while read searchterm <&3; do
            if grep -q "$searchterm" "$filename"; then
                if [ ! $first ]; then
                    terms="${terms},"
                fi
    
                first=0
                terms="${terms} $searchterm"
            fi
        done
        exec 3<&-
        if [ ! -z "$terms" ]; then
            echo "$filename: $terms"
        fi
    done
    It's a bit more verbose, but I feel like it has the following advantages:

    - Avoids needless use of cat by opening its own file descriptors (this is what exec does)
    - Avoids the extra comma at the beginning (the 'first' flag controls this)
    - Also properly quotes all parameters
    DISTRO=Arch
    Registered Linux User #388732

  4. #4
    Just Joined!
    Join Date
    Jul 2007
    Posts
    14
    thank you both much!

  5. #5
    scm
    scm is offline
    Linux Engineer
    Join Date
    Feb 2005
    Posts
    1,044
    And you can avoid the use of cat in the original by
    Code:
    for searchterm in $(< /home/searchterms.txt)

  6. #6
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    The problem with that approach is that it will use $IFS to separate values, and thus will split on spaces unless you redefine $IFS. For instance:
    Code:
    alex@danu ~ $ cat testfile
    Hello
    Hi
    Hooha
    Foo
    Bar
    Hello There
    alex@danu ~ $ cat asdf
    #!/bin/bash
    
    exec 3< testfile
    while read t <&3; do
        echo "$t"
    done
    alex@danu ~ $ ./asdf 
    Hello
    Hi
    Hooha
    Foo
    Bar
    Hello There
    alex@danu ~ $ vi asdf 
    alex@danu ~ $ cat asdf 
    #!/bin/bash
    
    for t in $(< testfile); do
        echo "$t"
    done
    alex@danu ~ $ ./asdf 
    Hello
    Hi
    Hooha
    Foo
    Bar
    Hello
    There
    In the first script run (using the exec approach), the script correctly identifies "Hello There" as a single line. In the second, because it splits the value on the space, it sees "Hello" and "There" as separate values.
    DISTRO=Arch
    Registered Linux User #388732

Posting Permissions

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