LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-27-2009, 06:50 AM   #1
sank1800
LQ Newbie
 
Registered: Feb 2009
Posts: 11

Rep: Reputation: 0
grep: Unmatched [ or [^


Hi,

I have a list of file names in a records.txt file. Each file name in it's own line and the file name format is as follows;

ex: 76739_DataRecord.00003

Here the first and last numbers have varying lengths in different file names. (7740_DataRecord.00002, 76739_DataRecord.0004 etc...)

Then there are log files in /var/log/ with the following format;

ex: DataLog.20090415

In these log files there are details about above file records (name, feed date etc...).

Now I want to grep the file names in the records.txt in all log files. So I am trying to use;

for i in `cat /tmp/records.txt`; do grep $i /var/log/DataLog* ; done

However when I execute this I get the "grep: Unmatched [ or [^ " error msg.

When doing manually I do this as follows and it works well.

grep "76739_DataRecord.00003" /var/log/DataLog*

What I want to know is how to get the same result when using the for loop. Since there are 2000+ file names it is difficult to do it manually.
 
Old 04-27-2009, 07:15 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Hi,

Could you tell a bit more? The above loop is correct and works (tried it), so something else must be wrong.

There is one minor issue, but that shouldn't result in your problem: Officially the $i in the grep statement should be surrounded by double quotes.

If possible, could you post the first few line of the records.txt file and one complete record in the log file?
 
Old 04-27-2009, 07:28 AM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
Is it possible that any of the entries have spaces? (Does each entry have "ex: " at the beginning?)

What happens if you run the loop with an "echo $i" statement? (Just to be sure what data is coming out of cat.)
 
Old 04-29-2009, 08:39 AM   #4
sank1800
LQ Newbie
 
Registered: Feb 2009
Posts: 11

Original Poster
Rep: Reputation: 0
Hi,

Thanks for the replies.

@druuna;
When you try the loop where there "_"s and "."s in the tested records. Because my guess is something happen to grep command when those characters are there.

@pixellany;
I checked and there are no spaces. When I check with echo it works fine.

Thanks again. Any suggestions?
 
Old 04-29-2009, 08:54 AM   #5
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
Quote:
Originally Posted by druuna View Post
If possible, could you post the first few line of the records.txt file and one complete record in the log file?
Also, does it matter what directory you are in when you run the command?

Last edited by pixellany; 04-29-2009 at 08:55 AM.
 
Old 04-29-2009, 09:17 AM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
The error message is explicit: unmatched [ or [^ means you have a square bracket in the search pattern, that is some of the file names in record.txt may contain "[". If this is the case try to use the -F option of grep, to force grep to interpret the pattern as a fixed string (not a regular expression):
Code:
grep -F $i /var/log/DataLog*
 
Old 04-29-2009, 09:39 AM   #7
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Hi,

Quote:
@druuna;
When you try the loop where there "_"s and "."s in the tested records. Because my guess is something happen to grep command when those characters are there.
I used your examples, so yes a _ and . where present.

How about the question I asked: If possible, could you post the first few line of the records.txt file and one complete record in the log file?
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
xargs: unmatched single quote akudewan Linux - Newbie 5 07-28-2014 01:16 PM
Logwatch unmatched entries courier mail collen Linux - Server 3 11-22-2006 11:07 AM
LogWatch Cron :: Unmatched Entries elvisious Linux - Software 2 06-23-2004 08:10 AM
xargs: unmatched single quote fillatre Linux - Software 2 06-05-2004 11:04 PM
Strange problem (Unmatched Entries) 2HostMe Linux - Newbie 2 01-10-2004 09:32 AM


All times are GMT -5. The time now is 02:08 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration