LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-09-2011, 05:45 PM   #1
aayushgoel
LQ Newbie
 
Registered: Nov 2011
Posts: 2

Rep: Reputation: Disabled
variable+grep+shell script issue.


Hi All,

The scenario is

I have a log file with some data related to Some IDs.
I want to grep data with those IDs and have to put it in another log file.
Also i am fetching the IDs from another log file.
Therefore i have created a loop to fetch IDs 1 by 1
and grep it, if it founds something it will write it in another log file.
But the script i have created is not working good


It is overwriting the data on the first line itself and and giving the data according to the last ID.

For your reference i am pasting my script.

#!/bin/bash

var=`cat mac1.logs`

for i in $var;
do
#grep -a -i -F "$i" EXCEPTIONS_n1_thru_n4.logs >> test_exceptions.logs
echo $i

done


Kindly help me with this.

Thanks,
Aayush
 
Old 11-09-2011, 06:49 PM   #2
Juako
Member
 
Registered: Mar 2010
Posts: 202

Rep: Reputation: 84
Without having a clue as to the files contents, I assume $var has several lines and that's why you are using -F with grep. Thing is, you already break the variable when you iterate with "for i in var".

Have you tried using grep -F directly over $var?

Also, if you are processing strings in binary files, have a look at the "strings" command, which searches and extracts strings in binaries.
 
Old 11-09-2011, 09:23 PM   #3
aayushgoel
LQ Newbie
 
Registered: Nov 2011
Posts: 2

Original Poster
Rep: Reputation: Disabled
Thanks for the reply Juako.

Var has many lines it contains only the IDs, each ID is in new line.
For example the content in mac1.logs is like
0000B699569C
0000B6996155

I tried -F with $var, it is working perfectly fine.

I think i was just making the stuff complex by applying loops.

Thanks for the help.

Aayush.
 
Old 11-10-2011, 12:09 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
I think you could also take out the var as well and just use the original file.
 
Old 11-10-2011, 12:35 AM   #5
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
yep, this is a total one-liner:
Code:
grep -F -f mac1.logs EXCEPTIONS_n1_thru_n4.logs > test_exceptions.logs
 
1 members found this post helpful.
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] grep --include glob with shell variable hashbang#! Programming 1 11-10-2010 04:02 PM
[SOLVED] grep for variable in a bash script wick_chad Programming 6 08-20-2010 10:16 AM
Pipe a variable using grep in a script. okos Linux - Software 6 08-17-2009 02:31 AM
grep a shell script variable contents kushalkoolwal Programming 8 02-04-2009 06:15 AM
Shell grep issue philipz Programming 8 05-05-2004 01:50 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:08 PM.

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
Open Source Consulting | Domain Registration