help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-bash] script help please


From: Stephane Chazelas
Subject: Re: [Help-bash] script help please
Date: Thu, 25 Jun 2015 17:34:56 +0100
User-agent: Mutt/1.5.21 (2010-09-15)

2015-06-25 18:35:47 +0300, Pierre Gaston:
> On Thu, Jun 25, 2015 at 5:38 PM, Randy <address@hidden> wrote:
> 
> >    I'm sorry this will seem like a simple question - but I've already
> >    asked my more bash-literate friends.  We're stuck.
> >    There is a command I use frequently:
> >    find *.pdf -exec sh -c 'pdftotext "{}" - | grep --with-filename
> >    --label="{}" --color "XXXX"' \;
> >    (where "XXXX" is the text for which I am search PDF files.)
> >    I tried to write a script, where I could simply pass-in ($1) the
> >    string.  Could not make it work.
> >    I would greatly appreciate help with this syntax.
> >    Thank you,
> >    Randy
> >
> 
> pass it and the filename as arguments to sh:
> 
> find *.pdf -exec sh -c 'pdftotext "$2" - | grep --with-filename
> --label="$2" --color -- "$1"'  _ "$1" {} \;

No need to run one sh per file.

#! /bin/sh -
exec find . -name '*.pdf' -type f -exec sh -c '
  regexp=$1; shift
  for file do
    pdftotext "$file" - |
      grep --with-filename --label="$file" --color -- "$regexp"
  done' sh "$1" {} +

Please avoid "_" for $0. That's used for error messages for
instance.

-- 
Stephane




reply via email to

[Prev in Thread] Current Thread [Next in Thread]