LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 09-19-2007, 10:19 AM   #1
qipman
LQ Newbie
 
Registered: Jul 2006
Posts: 14

Rep: Reputation: 0
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
 
Old 09-19-2007, 10:28 AM   #2
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,488

Rep: Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956
Code:
grep -vw
should do the trick. I don't know about compatibility with HP-UX, anyway.
 
Old 09-25-2007, 05:34 PM   #3
qipman
LQ Newbie
 
Registered: Jul 2006
Posts: 14

Original Poster
Rep: Reputation: 0
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?
 
Old 09-26-2007, 03:47 AM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
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.
 
Old 09-26-2007, 11:00 AM   #5
qipman
LQ Newbie
 
Registered: Jul 2006
Posts: 14

Original Poster
Rep: Reputation: 0
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
 
Old 09-26-2007, 12:01 PM   #6
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to remove first 2 lines of a file in a script nazs Programming 16 02-19-2007 07:08 AM
Random file lines directed to a new file. In script an error. In command line no err leventis Programming 1 09-28-2006 07:16 AM
Make a script remove lines from a file? spiffytech Linux - Software 5 12-29-2005 11:50 AM
removing lines from file script iluvatar Programming 9 08-20-2004 05:49 AM
Can't get lines of a file with a Bash script.. barisdemiray Programming 2 08-11-2004 12:42 PM


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

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration