LinuxQuestions.org
Visit Jeremy's Blog.
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 03-17-2009, 06:14 PM   #1
rose_bud4201
Member
 
Registered: Aug 2002
Location: St Louis, MO
Distribution: Xubuntu, RHEL, Solaris 10
Posts: 929

Rep: Reputation: 30
Grep in a for loop.


Can someone explain to me why this won't work? I know there's got to be something that I just can't see...

Code:
[516:laura@alderaan ~/crap/in_old]$ ll
total 26M
-rw-r--r-- 1 laura laura 131 2008-08-15 15:26 -1218831960
-rw-r--r-- 1 laura laura 138 2008-08-15 16:02 -1218834151
-rw-r--r-- 1 laura laura 130 2008-08-15 17:08 -1218838093
-rw-r--r-- 1 laura laura 153 2008-08-15 17:17 -1218838659
-rw-r--r-- 1 laura laura 140 2008-08-15 17:18 -1218838706
-rw-r--r-- 1 laura laura 153 2008-08-15 17:20 -1218838808
-rw-r--r-- 1 laura laura 142 2008-08-15 17:23 -1218838999
-rw-r--r-- 1 laura laura 142 2008-08-15 18:26 -1218842810
-rw-r--r-- 1 laura laura 140 2008-08-15 18:48 -1218844126
-rw-r--r-- 1 laura laura 142 2008-08-18 12:34 -1219080846
-rw-r--r-- 1 laura laura 142 2008-08-18 13:14 -1219083253
...
[517:laura@alderaan ~/crap/in_old]$ for file in $(ls); do grep "stuff" "$file"; done
grep: -1218831960: No such file or directory
grep: -1218834151: No such file or directory
grep: -1218838093: No such file or directory
grep: -1218838659: No such file or directory
grep: -1218838706: No such file or directory
grep: -1218838808: No such file or directory
grep: -1218838999: No such file or directory
grep: -1218842810: No such file or directory
grep: -1218844126: No such file or directory
grep: -1219080846: No such file or directory
grep: -1219083253: No such file or directory
...
It's not only because the files have ridiculous names - I can't grep the output of a directory listing in any other directory, either. It always says 'No such file or directory', even though the file is demonstrably *right there.*
 
Old 03-17-2009, 06:17 PM   #2
anomie
Senior Member
 
Registered: Nov 2004
Location: Texas
Distribution: RHEL, Scientific Linux, Debian, Fedora
Posts: 3,935
Blog Entries: 5

Rep: Reputation: Disabled
Try instead this form:
Code:
$ _i='./-9999' ; grep 'something' ${_i}
i.e.: Use either a relative or absolute path to the malformed filename. The leading '-' is screwing it up.
 
Old 03-17-2009, 06:26 PM   #3
rose_bud4201
Member
 
Registered: Aug 2002
Location: St Louis, MO
Distribution: Xubuntu, RHEL, Solaris 10
Posts: 929

Original Poster
Rep: Reputation: 30
It's *not* just the filename.

Code:
[527:laura@alderaan ~/crap]$ cd ..
[528:laura@alderaan ~/crap]$ ls Photo*
Photo_02.jpg  Photo_03.jpg  Photo_04.jpg
[529:laura@alderaan ~/crap]$ for file in $(ls Photo*); do grep "stuff" ${file}; done
grep: Photo_02.jpg: No such file or directory
grep: Photo_03.jpg: No such file or directory
grep: Photo_04.jpg: No such file or directory
grep: : No such file or directory
[530:laura@alderaan ~/crap]$ for file in $(ls Photo*); do grep "stuff" "/home/laura/crap/$file"; done
grep: /home/laura/crap/Photo_02.jpg: No such file or directory
grep: /home/laura/crap/Photo_03.jpg: No such file or directory
grep: /home/laura/crap/Photo_04.jpg: No such file or directory
grep: /home/laura/crap/: No such file or directory
 
Old 03-17-2009, 06:38 PM   #4
anomie
Senior Member
 
Registered: Nov 2004
Location: Texas
Distribution: RHEL, Scientific Linux, Debian, Fedora
Posts: 3,935
Blog Entries: 5

Rep: Reputation: Disabled
So start eliminating possibilities until you get to the root cause.
  • Does $ file /home/laura/crap/Photo_02.jpg give results?
  • Does $ grep 'stuff' /home/laura/crap/Photo_02.jpg by itself work?

Also post the output of $ echo $SHELL
 
Old 03-17-2009, 06:56 PM   #5
x_terminat_or_3
Member
 
Registered: Mar 2007
Location: Plymouth, UK
Distribution: Fedora Core, RHEL, Arch
Posts: 342

Rep: Reputation: 38
simple. . . the answer is, all those apparent files are broken symlinks.

Kindly paste the output of

ls -alh
 
Old 03-17-2009, 08:05 PM   #6
rose_bud4201
Member
 
Registered: Aug 2002
Location: St Louis, MO
Distribution: Xubuntu, RHEL, Solaris 10
Posts: 929

Original Poster
Rep: Reputation: 30
I'm at my computer at home now (same problem). They're not symlinks, broken or otherwise. Here's an example with some totally new files.

Code:
[510:laura@cleopatra ~/crap/test]$ echo "stuff" > testfile1
[511:laura@cleopatra ~/crap/test]$ echo "stuff" > testfile2
[512:laura@cleopatra ~/crap/test]$ echo "stuff" > testfile3
[513:laura@cleopatra ~/crap/test]$ ls -alh
total 20K
drwxr-xr-x 2 laura laura 4.0K 2009-03-17 18:35 .
drwxr-xr-x 7 laura laura 4.0K 2009-03-17 18:35 ..
-rw-r--r-- 1 laura laura    6 2009-03-17 18:35 testfile1
-rw-r--r-- 1 laura laura    6 2009-03-17 18:35 testfile2
-rw-r--r-- 1 laura laura    6 2009-03-17 18:35 testfile3
[514:laura@cleopatra ~/crap/test]$ for file in $(ls testfile*); do grep "stuff" ${file}; done
grep: testfile1: No such file or directory
grep: testfile2: No such file or directory
grep: testfile3: No such file or directory
grep: : No such file or directory
[515:laura@cleopatra ~/crap/test]$ file testfile1
testfile1: ASCII text
[516:laura@cleopatra ~/crap/test]$ grep stuff testfile1 
stuff
Code:
[517:laura@cleopatra ~/crap/test]$ echo $SHELL
/bin/bash
 
Old 03-17-2009, 08:48 PM   #7
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.3, Oracle Linux, Mint
Posts: 9,716

Rep: Reputation: 422Reputation: 422Reputation: 422Reputation: 422Reputation: 422
Try this as running "ls" is both unnecessary and a possible source of the problem:
Code:
for file in testfile*; do grep "stuff" ${file}; done
 
Old 03-17-2009, 09:16 PM   #8
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
using for loop is also unnecessary
Code:
grep "stuff" testfile*
 
Old 03-17-2009, 10:15 PM   #9
rose_bud4201
Member
 
Registered: Aug 2002
Location: St Louis, MO
Distribution: Xubuntu, RHEL, Solaris 10
Posts: 929

Original Poster
Rep: Reputation: 30
Smile

jlliagre and ghostdog74 - I did not know that trick! I thought the only way to find out what file the grep was finding "stuff" in was to use a for loop and echo the filename... excellent!

Many thanks to you both!
 
Old 03-17-2009, 11:34 PM   #10
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by ghostdog74 View Post
using for loop is also unnecessary
Code:
grep "stuff" testfile*
Well, yes and no - depends on the number of files into which the wildcard expands and on command line length limit.
 
Old 03-17-2009, 11:44 PM   #11
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 671Reputation: 671Reputation: 671Reputation: 671Reputation: 671Reputation: 671
Don't use filenames that start with hyphens. They look like arguments. If you run into one, you need to use two hyphens before the filename argument:

grep -- "something" file(s)

You also don't need the for loop because a wild card will work just as well.

A) grep -- 'pattern' *

B) for file in *; do
grep -- 'pattern' "$file"
done

Both of these would work. No reason to use the ls command.

Last edited by jschiwal; 03-17-2009 at 11:48 PM.
 
Old 03-18-2009, 01:06 AM   #12
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by Sergei Steshenko View Post
Well, yes and no - depends on the number of files into which the wildcard expands and on command line length limit.
well, then the venerable xargs come into play, still no need for a loop. A loop is needed when more complex operations need to be done on each line being grepped.
 
Old 03-18-2009, 01:42 AM   #13
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by ghostdog74 View Post
well, then the venerable xargs come into play, still no need for a loop. A loop is needed when more complex operations need to be done on each line being grepped.
Or just plain 'find' with -exec option.
 
Old 03-18-2009, 05:02 AM   #14
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by Sergei Steshenko View Post
Or just plain 'find' with -exec option.
For GNU find, building command lines using -exec command {} + will have the same effect as how xargs build its command line (man page). For "traditional" find without this option, using -exec, especially in this case we are talking about many many files , may not be as efficient as combining it with xargs.
 
  


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
how to loop over text file lines within bash script for loop? johnpaulodonnell Linux - Newbie 9 07-28-2015 04:49 PM
Trying to understand pipes - Can't pipe output from tail -f to grep then grep again lostjohnny Linux - Newbie 15 03-12-2009 11:31 PM
Grep result in a loop and linefeed issue romainp Programming 2 06-13-2008 11:11 AM
Bash loop using output of grep not working as needed Jim Pye Programming 7 01-16-2008 11:27 PM
problem with 'rpm -qa | grep' in while loop ionic_slim Programming 9 11-11-2007 09:09 AM

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

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