LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
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 07-12-2011, 01:42 AM   #1
Jykke
Member
 
Registered: Sep 2005
Posts: 168

Rep: Reputation: 16
Need a shell script help - grepping list of entries from another file?


I made this short shell script for having a file1 giving list of entries that I want to grep out of file2.

Code:
#!/bin/sh
for line in `cat $1`
do
 grep -A30 $line $2
 echo **--------------------------------------------------------
done
Unfortunately the grepping is not so exact as I might have characters like ; or space and so on in my search list. How can I improve it?
 
Old 07-12-2011, 02:12 AM   #2
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Please show us an example of the input file ($2 in your script) and the search list ($1) especially the not working part of them. Also take in mind that you can use the -f option of grep to read patterns from a file, so that you don't need to use a loop:
Code:
grep -A30 -f $1 $2
 
Old 07-12-2011, 04:59 AM   #3
Jykke
Member
 
Registered: Sep 2005
Posts: 168

Original Poster
Rep: Reputation: 16
Your example works straight out of the box and does not mess the stuff up like my trial. Thx.

However, I am still at least academically interested in the solution. So for this reason.
If file 1 has for example:
CONNECTOR BEHAVIOR, NAME=M1901479;SPW_WF-1901479

It begins by listing all lines from file2 where only word CONNECTOR is present and then with BEHAVIOR,
and then I believe it searches NAME=M1901479;SPW_WF-1901479

So all in all I think it is allergic to spaces in my file1 and treats a line as three separate strings
So perhaps it is in the layout of for - loop and using cat.
 
Old 07-12-2011, 08:40 AM   #4
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Quote:
Originally Posted by Jykke View Post
So all in all I think it is allergic to spaces in my file1 and treats a line as three separate strings. So perhaps it is in the layout of for - loop and using cat.
Yes. That's exactly the reason: the loop is iterated over the three fields separated by space. The shell uses the blank space (together with TAB and newline) to separate fields (check the IFS built-in variable). You can alter the value of IFS to protect the space from being considered the field separator or - better - you can do:
Code:
while read line
do
  something with "$line"
done < $1
In this case the read statement takes care of assigning the whole line to the loop variable (despite the presence of blank spaces or any other special symbol). Just put the reference to $line inside double quotes when you use it.
 
  


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
Shell script calling shell script - List of all nikunjbadjatya Programming 7 04-13-2011 07:27 PM
Shell script for monitoring a list of IP's HariharanV Linux - Software 26 09-20-2010 07:39 AM
Shell script to install list of packages in a file jayasekar Linux - Software 4 01-22-2010 02:56 AM
[SOLVED] Need a shell script "grepping" IP adress, and act according to this IP. Linux.tar.gz Programming 15 11-06-2009 11:27 AM
Shell script to list all users in /etc/passwd file milestone Programming 6 09-15-2007 01:57 PM


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

Main Menu
Advertisement
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