Home > Archive > AWK > June 2004 > text/value substitution in a single line
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 |
text/value substitution in a single line
|
|
| Bullfrog528 2004-06-10, 8:57 pm |
| Hi
I'm new to awk,not entirely sure if this is something awk can resolve
for me or perhaps sed or perhaps someone has some other bright ideas.
I've a script containing a variable containing values seperated by
asterix.
i.e.
#!/bin/sh
master="no*yes*no*yes*mand*dummy"
I want to have a function or procedure where I can say position 3 =
yes
If the current value is mand or dummy I can't change it otherwise it
takes on it's new value.
so the example above master becomes
"no*yes*yes*yes*mand*dummy"
If I were to say position 5 = yes it would ignore me.
Anyone have any idea how I can set this up without having to manually
parse the variable each time using some script lines.
Hopefully I've made the above clear enough
Thanks
Jeremy
| |
| Ed Morton 2004-06-10, 8:57 pm |
|
Bullfrog528 wrote:
> Hi
>
> I'm new to awk,not entirely sure if this is something awk can resolve
> for me or perhaps sed or perhaps someone has some other bright ideas.
>
> I've a script containing a variable containing values seperated by
> asterix.
> i.e.
>
> #!/bin/sh
> master="no*yes*no*yes*mand*dummy"
>
> I want to have a function or procedure where I can say position 3 =
> yes
> If the current value is mand or dummy I can't change it otherwise it
> takes on it's new value.
>
> so the example above master becomes
> "no*yes*yes*yes*mand*dummy"
>
> If I were to say position 5 = yes it would ignore me.
>
> Anyone have any idea how I can set this up without having to manually
> parse the variable each time using some script lines.
>
> Hopefully I've made the above clear enough
Try this:
awk -F'*' -vp=3 '($p != "mand")&&($p != "dummy"){$p="yes"}{print}'
Just change the value of "p" for different fields.
Regards,
Ed
> Thanks
>
> Jeremy
| |
| Patrick TJ McPhee 2004-06-10, 8:57 pm |
| In article <967dee32.0406101350.49210849@posting.google.com>,
Bullfrog528 <google@harkcom.co.uk> wrote:
% I'm new to awk,not entirely sure if this is something awk can resolve
% for me or perhaps sed or perhaps someone has some other bright ideas.
You do this in the shell itself.
% #!/bin/sh
% master="no*yes*no*yes*mand*dummy"
# Follow this up with
IFS='*'
set $master
if [ "$3" = yes ]
then
echo $5
fi
--
Patrick TJ McPhee
East York Canada
ptjm@interlog.com
| |
| Ed Morton 2004-06-11, 3:55 am |
|
Patrick TJ McPhee wrote:
> In article <967dee32.0406101350.49210849@posting.google.com>,
> Bullfrog528 <google@harkcom.co.uk> wrote:
>
> % I'm new to awk,not entirely sure if this is something awk can resolve
> % for me or perhaps sed or perhaps someone has some other bright ideas.
>
> You do this in the shell itself.
>
> % #!/bin/sh
> % master="no*yes*no*yes*mand*dummy"
>
> # Follow this up with
> IFS='*'
> set $master
>
> if [ "$3" = yes ]
> then
> echo $5
> fi
>
One of us ( and I'm hoping it's you ;-) ) needs to re-read the OP as the
above isn't remotely like what I thought he was asking for.
Ed.
| |
| Bullfrog528 2004-06-12, 8:55 pm |
| Ed Morton <morton@lsupcaemnt.com> wrote in message news:<M9Sdnc4pa81pZlXdRVn-hw@comcast.com>...
> Patrick TJ McPhee wrote:
>
>
> One of us ( and I'm hoping it's you ;-) ) needs to re-read the OP as the
> above isn't remotely like what I thought he was asking for.
>
> Ed.
Thanks Ed
I'll give your awk command a go.Yes this is what I wanted.Think the
second poster mis-read the question somewhere.
Regards
Jeremy
| |
| Patrick TJ McPhee 2004-06-12, 8:55 pm |
| In article <967dee32.0406121341.2abb38fc@posting.google.com>,
Bullfrog528 <google@harkcom.co.uk> wrote:
% I'll give your awk command a go.Yes this is what I wanted.Think the
% second poster mis-read the question somewhere.
No, I wasn't providing a solution to your problem, just showing you
how to get at the individual field data in the shell. If it doesn't
get you thinking `oh, I could use that', then fine, there was already
a seemingly-complete awk solution.
--
Patrick TJ McPhee
East York Canada
ptjm@interlog.com
| |
| Bullfrog528 2004-06-13, 8:55 am |
| ptjm@interlog.com (Patrick TJ McPhee) wrote in message news:<cag5oc$ja3$1@news.eusc.inter.net>...
> In article <967dee32.0406121341.2abb38fc@posting.google.com>,
> Bullfrog528 <google@harkcom.co.uk> wrote:
>
> % I'll give your awk command a go.Yes this is what I wanted.Think the
> % second poster mis-read the question somewhere.
>
> No, I wasn't providing a solution to your problem, just showing you
> how to get at the individual field data in the shell. If it doesn't
> get you thinking `oh, I could use that', then fine, there was already
> a seemingly-complete awk solution.
Thanks Patrick.Always good to learn new ways of doing something.At the
moment I'm using
value=`echo "$master"|cut -f3 -d"*"` to get the third value for
instance.
your method appears more efficient so I'll look into this for
future scripts.
Ed. Back to the awk method - the returned string is missing the
astrix's,finally I wish to pass the value to be set as a parameter too
(parameter = yes or no).I've tried {$p=$param}{print} and
{$p="$param"}{print} at the end of the line without success.Admittedly
I've got around this using a test and 2 similar lines - can't see how
to get around the missing astrix's from the returned value though,any
thoughts.
My test script looks like this at the moment.
#!/bin/sh
setvalue()
{
[ "$param" = "yes" ] && master=`echo "$master"|awk -F'*' -vp=$pos '($p
!= "mand")&&($p !="dummy"){$p="yes"}{print}'`
[ "$param" = "no" ] && master=`echo "$master"|awk -F'*' -vp=$pos '($p
!= "mand")&&($p !="dummy"){$p="no"}{print}'`
}
#
master="yes*no*yes*dummy*mand*yes"
#
#
#
echo "$master"
pos=2
param="yes"
setvalue
echo "$pos $param"
echo "$master"
pos=1
param="fred"
setvalue
echo "$pos $param"
echo "$master"
pos=4
param="no"
setvalue
echo "$pos $param"
echo "$master"
Thanks again both of you.
Jeremy
| |
| Bullfrog528 2004-06-13, 8:55 am |
| ptjm@interlog.com (Patrick TJ McPhee) wrote in message news:<cag5oc$ja3$1@news.eusc.inter.net>...
> In article <967dee32.0406121341.2abb38fc@posting.google.com>,
> Bullfrog528 <google@harkcom.co.uk> wrote:
>
> % I'll give your awk command a go.Yes this is what I wanted.Think the
> % second poster mis-read the question somewhere.
>
> No, I wasn't providing a solution to your problem, just showing you
> how to get at the individual field data in the shell. If it doesn't
> get you thinking `oh, I could use that', then fine, there was already
> a seemingly-complete awk solution.
Hi I've got around the missing asterix's by replacing {$p="yes") and
($p="no") with {OFS="*" ; $p="yes") and (OFS="*" ; $p="no")
respectively.
In order to combine my 2 awk lines into one,I assume I need to pass
more than one variable using the -v command but can't see how this is
done.
Thanks again
Jeremy
| |
| Ed Morton 2004-06-13, 3:56 pm |
|
Bullfrog528 wrote:
> ptjm@interlog.com (Patrick TJ McPhee) wrote in message news:<cag5oc$ja3$1@news.eusc.inter.net>...
>
>
>
> Thanks Patrick.Always good to learn new ways of doing something.At the
> moment I'm using
>
> value=`echo "$master"|cut -f3 -d"*"` to get the third value for
> instance.
>
> your method appears more efficient so I'll look into this for
> future scripts.
>
> Ed. Back to the awk method - the returned string is missing the
> astrix's,
Set OFS=FS.
finally I wish to pass the value to be set as a parameter too
> (parameter = yes or no).I've tried {$p=$param}{print} and
> {$p="$param"}{print} at the end of the line without success.
The value of p is "3" while the value of param is, say, "yes". Bearing
in mind that putting a "$" before a number (or a variable whose value is
a number) tells awk that you're refering to a field of that number in
the current line, if you said:
$p = $param
that'd be saying:
set field number "3" to field number "yes"
and, of course, there is no field number "yes". Instead you want to say
set field number "3" to the value of the variable "param" (i.e. "yes")
which is just:
$p = param
Admittedly
> I've got around this using a test and 2 similar lines - can't see how
> to get around the missing astrix's from the returned value though,any
> thoughts.
>
> My test script looks like this at the moment.
>
> #!/bin/sh
> setvalue()
> {
> [ "$param" = "yes" ] && master=`echo "$master"|awk -F'*' -vp=$pos '($p
> != "mand")&&($p !="dummy"){$p="yes"}{print}'`
> [ "$param" = "no" ] && master=`echo "$master"|awk -F'*' -vp=$pos '($p
> != "mand")&&($p !="dummy"){$p="no"}{print}'`
> }
Change the above to:
setvalue()
{
master=`echo "$master" |
awk -vpos="$pos" -vparam="$param" '
BEGIN{FS=OFS="*"}
($pos != "mand")&&($pos != "dummy"){$pos=param}
{print}'`
}
I changed "p" to "pos" for consistency since both parameters start with
"p". I moved the FS setting inside BEGIN so the FS and OFS settings
would be together for clarity/maintainability.
Regards,
Ed.
|
|
|
|
|