From: noelloen on
Hi

I wish to have multiply shell variable in one awk statement

eg,
without using shell variable
awk 'NR>2 && NR<=10' $1> new_$1

$1 is a shell variable which is the input_data filename, new_$1 is the
output, this above statment works but , i have trouble getting the
following


echo $lineOne #lineOne is 2
echo $lineTwo #lineTwo is 10

case 1 )
awk 'NR>'$lineOne' && NR<='$lineTwo' ' $1> new_$1

it would not work.

case 2)
awk 'NR>awkvar && NR<=awkvar2' awkvar=$lineOne awkvar2=$lineTwo $1 >
new_$1

it would not work.

case 3)
awk -v awkvar=$lineOne awkvar2=$lineTwo 'NR>awkvar && NR<=awkvar2' $1 >
new_$1

it would not work.

I have using ksh here..


Please help me out with this, since all 3 cases i tried are not
working.


Thanks

From: Xicheng Jia on
noelloen wrote:
> Hi
>
> I wish to have multiply shell variable in one awk statement
>
> eg,
> without using shell variable
> awk 'NR>2 && NR<=10' $1> new_$1
>
> $1 is a shell variable which is the input_data filename, new_$1 is the
> output, this above statment works but , i have trouble getting the
> following
>
>
> echo $lineOne #lineOne is 2
> echo $lineTwo #lineTwo is 10
>
> case 1 )
> awk 'NR>'$lineOne' && NR<='$lineTwo' ' $1> new_$1
>
> it would not work.
>
> case 2)
> awk 'NR>awkvar && NR<=awkvar2' awkvar=$lineOne awkvar2=$lineTwo $1 >
> new_$1
>
> it would not work.
>
> case 3)
> awk -v awkvar=$lineOne awkvar2=$lineTwo 'NR>awkvar && NR<=awkvar2' $1 >
> new_$1
>
> it would not work.

If no whitespace in "$1" and the following is true:
echo $lineOne #lineOne is 2
2
echo $lineTwo #lineTwo is 10
10

then, case1/case2 looks fine, but case3 should be:

awk -v awkvar="$lineOne" -v awkvar2="$lineTwo" '
NR > awkvar && NR <= awkvar2
' "$1" > "new_$1"

you need to add another '-v' option in your 2nd initialized awk
variable. (BTW. better add quotation marks on the shell variables)

Regards,
Xicheng

From: noelloen on
But case 1 nor case 2 are working
...
I wonder how come?


Xicheng Jia wrote:
> noelloen wrote:
> > Hi
> >
> > I wish to have multiply shell variable in one awk statement
> >
> > eg,
> > without using shell variable
> > awk 'NR>2 && NR<=10' $1> new_$1
> >
> > $1 is a shell variable which is the input_data filename, new_$1 is the
> > output, this above statment works but , i have trouble getting the
> > following
> >
> >
> > echo $lineOne #lineOne is 2
> > echo $lineTwo #lineTwo is 10
> >
> > case 1 )
> > awk 'NR>'$lineOne' && NR<='$lineTwo' ' $1> new_$1
> >
> > it would not work.
> >
> > case 2)
> > awk 'NR>awkvar && NR<=awkvar2' awkvar=$lineOne awkvar2=$lineTwo $1 >
> > new_$1
> >
> > it would not work.
> >
> > case 3)
> > awk -v awkvar=$lineOne awkvar2=$lineTwo 'NR>awkvar && NR<=awkvar2' $1 >
> > new_$1
> >
> > it would not work.
>
> If no whitespace in "$1" and the following is true:
> echo $lineOne #lineOne is 2
> 2
> echo $lineTwo #lineTwo is 10
> 10
>
> then, case1/case2 looks fine, but case3 should be:
>
> awk -v awkvar="$lineOne" -v awkvar2="$lineTwo" '
> NR > awkvar && NR <= awkvar2
> ' "$1" > "new_$1"
>
> you need to add another '-v' option in your 2nd initialized awk
> variable. (BTW. better add quotation marks on the shell variables)
>
> Regards,
> Xicheng

From: noelloen on
Oh. I figured it out.

There was something wrong with $lineOne,
so, I use another awk to extract the $lineOne first and now case 1
works.

lineOne2=`echo $lineOne | awk '{print $1}'`
echo $lineOne2 #gives 2

I guess there was space behind 2.

Thank you.

noelloen wrote:
> But case 1 nor case 2 are working
> ..
> I wonder how come?
>
>
> Xicheng Jia wrote:
> > noelloen wrote:
> > > Hi
> > >
> > > I wish to have multiply shell variable in one awk statement
> > >
> > > eg,
> > > without using shell variable
> > > awk 'NR>2 && NR<=10' $1> new_$1
> > >
> > > $1 is a shell variable which is the input_data filename, new_$1 is the
> > > output, this above statment works but , i have trouble getting the
> > > following
> > >
> > >
> > > echo $lineOne #lineOne is 2
> > > echo $lineTwo #lineTwo is 10
> > >
> > > case 1 )
> > > awk 'NR>'$lineOne' && NR<='$lineTwo' ' $1> new_$1
> > >
> > > it would not work.
> > >
> > > case 2)
> > > awk 'NR>awkvar && NR<=awkvar2' awkvar=$lineOne awkvar2=$lineTwo $1 >
> > > new_$1
> > >
> > > it would not work.
> > >
> > > case 3)
> > > awk -v awkvar=$lineOne awkvar2=$lineTwo 'NR>awkvar && NR<=awkvar2' $1 >
> > > new_$1
> > >
> > > it would not work.
> >
> > If no whitespace in "$1" and the following is true:
> > echo $lineOne #lineOne is 2
> > 2
> > echo $lineTwo #lineTwo is 10
> > 10
> >
> > then, case1/case2 looks fine, but case3 should be:
> >
> > awk -v awkvar="$lineOne" -v awkvar2="$lineTwo" '
> > NR > awkvar && NR <= awkvar2
> > ' "$1" > "new_$1"
> >
> > you need to add another '-v' option in your 2nd initialized awk
> > variable. (BTW. better add quotation marks on the shell variables)
> >
> > Regards,
> > Xicheng

 | 
Pages: 1
Prev: Keyboard Input
Next: mutt macro and little script