|
Home > Archive > Unix Shell > September 2006 > substituing $1 one time but not the other
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
substituing $1 one time but not the other
|
|
| patrice 2006-09-27, 1:21 pm |
| Hello
I want to get the name of a mail file using its message ID.
I 'm using :
grep '^Message-ID: <my id>$' myfolder/* | awk -F ':' '{print $1;}'
and it work fine.
now i want to put this line in a shell script with 2 params: myid and
myfolder
so :
grep '^Message-ID: <$1>$' $2/* | awk -F ':' '{print $1;}'
but it does not work because of the awk argument 'print $1 ' .
the command line is substitued by
grep '^Message-ID: <myid>$' myfolder/* | awk -F ':' '{print myid;}'
how can i tell the shell to not substitute the print $1 ?????
| |
| Ed Morton 2006-09-27, 1:21 pm |
| patrice wrote:
> Hello
>
> I want to get the name of a mail file using its message ID.
> I 'm using :
> grep '^Message-ID: <my id>$' myfolder/* | awk -F ':' '{print $1;}'
> and it work fine.
> now i want to put this line in a shell script with 2 params: myid and
> myfolder
>
> so :
> grep '^Message-ID: <$1>$' $2/* | awk -F ':' '{print $1;}'
>
> but it does not work because of the awk argument 'print $1 ' .
No, it doesn't work because the shell variable $1 is inside single
quotes and so won't be expanded.
> the command line is substitued by
> grep '^Message-ID: <myid>$' myfolder/* | awk -F ':' '{print myid;}'
>
> how can i tell the shell to not substitute the print $1 ?????
I don't really know what you mean by that question, but everything
before it seems to imply that what you to is instead of this:
grep '^Message-ID: <my id>$' myfolder/* | awk -F ':' '{print $1;}'
be able to do this:
myid="$1"
myfolder="$2"
grep "^Message-ID: ${myid}$" "${myfolder}"/* | awk -F ':' '{print $1;}'
By the way you don't need grep AND awk since awk can cheerfully do
pattern matching on it's own:
myid="$1"
myfolder="$2"
awk -F: -v myid="$myid" '$0 ~ "^Message-ID: " myid "$"{print $1}'
"${myfolder}"/*
Regards,
Ed.
| |
| Ed Morton 2006-09-27, 1:21 pm |
| Ed Morton wrote:
<snip>
missed those pesky "<>"s. fixed below.
> I don't really know what you mean by that question, but everything
> before it seems to imply that what you to is instead of this:
>
> grep '^Message-ID: <my id>$' myfolder/* | awk -F ':' '{print $1;}'
>
> be able to do this:
>
> myid="$1"
> myfolder="$2"
> grep "^Message-ID: ${myid}$" "${myfolder}"/* | awk -F ':' '{print $1;}'
grep "^Message-ID: <${myid}>$" "${myfolder}"/* | awk -F ':' '{print $1}'
>
> By the way you don't need grep AND awk since awk can cheerfully do
> pattern matching on it's own:
>
> myid="$1"
> myfolder="$2"
> awk -F: -v myid="$myid" '$0 ~ "^Message-ID: " myid "$"{print $1}'
> "${myfolder}"/*
awk -F: -v myid="$myid" '$0 ~ "^Message-ID: <" myid ">$"{print $1}'
"${myfolder}"/*
>
> Regards,
>
> Ed.
| |
| Barry Margolin 2006-09-27, 7:37 pm |
| In article <451aaa63$0$1959$626a54ce@news.free.fr>,
"patrice" <patrice_labracherie_nospam@free.fr> wrote:
> Hello
>
> I want to get the name of a mail file using its message ID.
> I 'm using :
> grep '^Message-ID: <my id>$' myfolder/* | awk -F ':' '{print $1;}'
> and it work fine.
> now i want to put this line in a shell script with 2 params: myid and
> myfolder
>
> so :
> grep '^Message-ID: <$1>$' $2/* | awk -F ':' '{print $1;}'
>
> but it does not work because of the awk argument 'print $1 ' .
> the command line is substitued by
> grep '^Message-ID: <myid>$' myfolder/* | awk -F ':' '{print myid;}'
>
> how can i tell the shell to not substitute the print $1 ?????
Actually, the command line that's being executed is:
grep '^Message-ID: <$1>$' myfolder/* | awk -F ':' '{print $1;}'
The reason is that single quotes prevent variable substituion, so $1
isn't being replaced in the grep argument. So you need to use double
quotes around the grep argument:
grep "^Message-ID: <$1>\$" myfolder/* | awk -F ':' '{print $1;}'
BTW, you don't need to use awk to get the filename, just use the -l
option to grep to make it print out just the names of the files with
matching lines.
--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
| |
| Stephane CHAZELAS 2006-09-27, 7:37 pm |
| 2006-09-27, 18:44(+02), patrice:
> Hello
>
> I want to get the name of a mail file using its message ID.
> I 'm using :
> grep '^Message-ID: <my id>$' myfolder/* | awk -F ':' '{print $1;}'
> and it work fine.
> now i want to put this line in a shell script with 2 params: myid and
> myfolder
>
> so :
> grep '^Message-ID: <$1>$' $2/* | awk -F ':' '{print $1;}'
[...]
Looks like you want
grep -l "^Message-ID: <$1>\$" -- "$2"/*
--
Stéphane
|
|
|
|
|