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 ...
- 08-13-2007 #1Just 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?
I want the output to look like thisCode: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
1089-006.doc : packet, radio
instead of
1089-006.doc : packet
1089-006.doc : radio
- 08-13-2007 #2Linux 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.
The output will end up looking like this: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
1089-006.doc : , packet, radio
Use something like awk's substr function to remove that unwanted 1st comma.
Vic
- 08-13-2007 #3
I prefer this:
It's a bit more verbose, but I feel like it has the following advantages: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
- 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 parametersDISTRO=Arch
Registered Linux User #388732
- 08-14-2007 #4Just Joined!
- Join Date
- Jul 2007
- Posts
- 14
thank you both much!
- 08-14-2007 #5Linux 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)
- 08-15-2007 #6
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:
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.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 ThereDISTRO=Arch
Registered Linux User #388732


Reply With Quote