Re: xargs - is it possible to take each argument in to xargs and use in sed against a file?
From: Stephane Chazelas (stephane_at_artesyncp.com)
Date: 08/19/05
- Next message: dave_at_geol.invalid: "Re: hidding the content of a script"
- Previous message: Bruce Barnett: "Re: I want to monitor the creation and deletion of PDFs"
- In reply to: Dundonald: "Re: xargs - is it possible to take each argument in to xargs and use in sed against a file?"
- Next in thread: Dundonald: "Re: xargs - is it possible to take each argument in to xargs and use in sed against a file?"
- Reply: Dundonald: "Re: xargs - is it possible to take each argument in to xargs and use in sed against a file?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 19 Aug 2005 20:16:10 GMT
On 19 Aug 2005 12:56:46 -0700, Dundonald wrote:
[...]
> awk < $file '
> /$criteria/ && FNR > 1 {
> print last_line
> }
> {
> last_line = $0
> }'
You need to have the shell pass the variable to awk.
A good way to do this is making the variable an environment
variable have use awk's special ENVIRON associative array.
criteria_ENV=$criteria awk < "$file" '
$0 ~ ENVIRON["criteria_ENV"] {
print last_line
}
{
last_line = $0
}'
You need a POSIX conformant awk. That should be fine on every
recent Unices, except on Solaris where you need to make sure
/usr/xpg4/bin/awk is used and not the legacy /usr/bin/awk.
An other solution is to pass it as an argument and use the ARGV
special array in awk. There are other solutions using the -v
option of awk or the old var=value awk argument convention, or
have the shell expand the value of the variable within the awk
code argument, but none of them are suitable for a regexp as
$criteria is as they won't work if special characters are
present in the variable (and those special characters are often
found in regexps).
-- Stephane
- Next message: dave_at_geol.invalid: "Re: hidding the content of a script"
- Previous message: Bruce Barnett: "Re: I want to monitor the creation and deletion of PDFs"
- In reply to: Dundonald: "Re: xargs - is it possible to take each argument in to xargs and use in sed against a file?"
- Next in thread: Dundonald: "Re: xargs - is it possible to take each argument in to xargs and use in sed against a file?"
- Reply: Dundonald: "Re: xargs - is it possible to take each argument in to xargs and use in sed against a file?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|