LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (http://www.linuxquestions.org/questions/linux-general-1/)
-   -   script to grep lines out of a file (http://www.linuxquestions.org/questions/linux-general-1/script-to-grep-lines-out-of-a-file-585828/)

qipman 09-19-2007 10:19 AM

script to grep lines out of a file
 
Hello.
I need to configure the below script so that it removes lines from a file that contain words from an input file. As it is now, if you were to place the text one, two and three each on its own line in the input file, instances if onea, oneb, and onec would be also removed (when only lines containing “one” “teo” or ‘three” need to be removed).
Since $i is a variable, quotes or ticks will not help me here. I need this script to be compatible with Linux and HP UX. Any ideas?

FILE=main_file
INPUT=input_file

# greps out lines from the input file and rewrites to output file:
for i in `cat $INPUT`
do
cat $FILE | grep -v $i > out
cat out > $FILE
done

colucix 09-19-2007 10:28 AM

Code:

grep -vw
should do the trick. I don't know about compatibility with HP-UX, anyway.

qipman 09-25-2007 05:34 PM

Thanks much, that did the trick.

Now I am in need of a script that will grep out lines from a file using data from an input file and then write the data to an out file. For example, I have a file with 1000 entries, and I need to grep out all lines that contain lets say, 100 hostnames (unique) and write to an output file just to confirm that the entries do exist there. Here is what I have so far, but the output file gets the same data written to it for each time that the script reads the main file:

#!/bin/sh
FILE=main_file
INPUT=grep_input_file
OUT=rpt_grep_output
> $OUT
# greps out lines from the input file and writes to output file:
for i in `cat $INPUT`
do
cat $FILE | grep -iw $i >> $OUT
done


Any thoughts?

ghostdog74 09-26-2007 03:47 AM

from the way you do it, you will have to cat the file 1000 times inside the for loop. Inefficient I/O operation and costly. Show a sample of your 2 files and what you expect to see as output. describe your problem as clearly as possible. There should be a better way to do what you want.

qipman 09-26-2007 11:00 AM

Ok,
This would be a script to grep out lines from a file, let's say a zone file, containing words from an input file. The script will output the lines to an output file

The script:

#!/bin/sh
FILE=main_file
INPUT=grep_input_file
OUT=rpt_grep_output
> $OUT
# greps out lines from the input file and writes to output file:
for i in `cat $INPUT`
do
cat $FILE | grep -iw $i >> $OUT
done

The "main" file being searched, main_file:

host IN A 10.0.0.1
host1 IN A 10.0.0.2
host2 IN A 10.0.0.3
host3 IN A 10.0.0.4
host4 IN A 10.0.0.5
host5 IN A 10.0.0.6

The file containing the search words, grep_input_file:

host
host2

What is expected in the output file (leaving the data intact in the main_file, of course):

host IN A 10.0.0.1
host2 IN A 10.0.0.3

ghostdog74 09-26-2007 12:01 PM

here it is, just awk.:
Code:

awk 'FNR==NR{search[$1]=$0;next}
    {print search[$0]}
' "file1" "file2"

output:
Code:

# ./test.sh
host IN A 10.0.0.1
host2 IN A 10.0.0.3



All times are GMT -5. The time now is 10:35 AM.