[Linux-disciples] Grep oddity

Adam Rosi-Kessel adam at rosi-kessel.org
Fri Apr 15 10:04:23 EDT 2005


Yes, it's because stderr is different from stdout.

Add 2>&1 and your problem will be solved.

I would take a different approach, though:

for x in $(grep -o "[0-9]{3}\.jpg" filename.html)
do
  if [ ! -e dirname/$x ]
  then
     echo File $x does not exist
  fi
done

(untested)

On Fri, Apr 15, 2005 at 08:10:23AM -0400, Stephen R Laniel wrote:
> I have a bunch of links of the form
> 
> <img src="dirname/[0-9]{3}\.jpg"
> 
> in an HTML file (call it filename.html). A client reports
> that many of those links are showing up as missing
> graphics in Internet Explorer. I'd prefer not to go
> through those images one by one and figure out which ones
> are missing.
> 
> So I'm trying to run a command like so:
> 
> grep -o "[0-9]{3}\.jpg" filename.html |xargs -i [ ! -e dirname/{} ] && echo "File {} does not exist"
> 
> That didn't work, which must be because I don't really
> understand the [...] syntax. So I tried a hack:
> 
> grep -o "[0-9]{3}\.jpg" filename.html |xargs -i ls dirname/{} |grep -i 'no such file or directory' 2>&1 |grep -o '[0-9]{3}\.jpg'
> 
> But it's not working: it's displaying the entire line, not
> just the matching bit (which is what the '-o' is supposed to
> do). I've tried doing the last pipe also with sed (cutting
> out everything from the beginning of the line up to
> '[0-9]{3}\.jpg' and everything from that regex to the end of
> the line), but that also fails.
> 
> There must be something really simple that I'm missing. Care
> to help a clueless hacker?
-- 
Adam Rosi-Kessel
http://adam.rosi-kessel.org


More information about the Linux-disciples mailing list