Print xth column of yth line

  • Follow


I have a file having fields in a matrix fashion.
What are the different ways in which I can print 3rd column of 5th row
(i.e. NF =3  and line = 5)??

Thanks
Awk baby
0
Reply whereismelvin (5) 11/26/2009 6:22:07 AM

Perl baby wrote:
> I have a file having fields in a matrix fashion.
> What are the different ways in which I can print 3rd column of 5th row
> (i.e. NF =3  and line = 5)??
> 

How many different ways of printing would you need?

Hermann
0
Reply Hermann 11/26/2009 7:19:17 AM


On Nov 26, 12:19 pm, Hermann Peifer <pei...@gmx.eu> wrote:
> Perl baby wrote:
> > I have a file having fields in a matrix fashion.
> > What are the different ways in which I can print 3rd column of 5th row
> > (i.e. NF =3  and line = 5)??
>
> How many different ways of printing would you need?
>
> Hermann

Wud be gr8 if u cud gimme atleast one.

Thanks
Awk Baby
0
Reply Perl 11/26/2009 8:14:31 AM

On Thu, 26 Nov 2009 00:14:31 -0800 (PST), Perl baby <whereismelvin@gmail.com> wrote:

>On Nov 26, 12:19 pm, Hermann Peifer <pei...@gmx.eu> wrote:
>> Perl baby wrote:
>> > I have a file having fields in a matrix fashion.
>> > What are the different ways in which I can print 3rd column of 5th row
>> > (i.e. NF =3  and line = 5)??
>>
>> How many different ways of printing would you need?
>>
>> Hermann
>
>Wud be gr8 if u cud gimme atleast one.

Like:

  awk 'NR == 5 {print $3}' file

Grant.
-- 
http://bugsplatter.id.au
0
Reply Grant 11/26/2009 8:27:55 AM

Perl baby wrote:
> On Nov 26, 12:19 pm, Hermann Peifer <pei...@gmx.eu> wrote:
>> Perl baby wrote:
>>> I have a file having fields in a matrix fashion.
>>> What are the different ways in which I can print 3rd column of 5th row
>>> (i.e. NF =3  and line = 5)??
>> How many different ways of printing would you need?
>>
>> Hermann
> 
> Wud be gr8 if u cud gimme atleast one.
> 

Try out how far you get with

awk 'NR==5{print $3}' file

Hermann
0
Reply Hermann 11/26/2009 8:30:21 AM

Thu, 26 Nov 2009 19:27:55 +1100, Grant did cat :

> On Thu, 26 Nov 2009 00:14:31 -0800 (PST), Perl baby
> <whereismelvin@gmail.com> wrote:
> 
>>On Nov 26, 12:19 pm, Hermann Peifer <pei...@gmx.eu> wrote:
>>> Perl baby wrote:
>>> > I have a file having fields in a matrix fashion. What are the
>>> > different ways in which I can print 3rd column of 5th row (i.e. NF
>>> > =3  and line = 5)??
>>>
>>> How many different ways of printing would you need?
>>>
>>> Hermann
>>
>>Wud be gr8 if u cud gimme atleast one.
> 
> Like:
> 
>   awk 'NR == 5 {print $3}' file
> 
> Grant.

 let's play "flog" for a change ;-)
---------
awk -v col=3 -v row=5 '
FNR>row{
	print a
	exit
}
FNR!=row{	next}
{
	for(i=NF;i;--i){
		if(i>col){continue}
		if(i<col){break}
		a=$(i)
	}
}
' yerfile
---------
0
Reply Loki 11/26/2009 11:46:57 AM

Loki Harfagr wrote:
>---------
>awk -v col=3 -v row=5 '
>FNR>row{
>	print a
>	exit
>}
>FNR!=row{	next}
>{
>	for(i=NF;i;--i){
>		if(i>col){continue}
>		if(i<col){break}
>		a=$(i)
>	}
>}
>' yerfile
>---------


awk -v col=3 -v row=5 'FNR==row {print $(col); exit}' myfile
-- 

Lorenz
0
Reply Lorenz 11/26/2009 1:03:22 PM

Thu, 26 Nov 2009 13:03:22 +0000, Lorenz did cat :

> Loki Harfagr wrote:
>>---------
>>awk -v col=3 -v row=5 '
>>FNR>row{
>>	print a
>>	exit
>>}
>>FNR!=row{	next}
>>{
>>	for(i=NF;i;--i){
>>		if(i>col){continue}
>>		if(i<col){break}
>>		a=$(i)
>>	}
>>}
>>' yerfile
>>---------
> 
> 
> awk -v col=3 -v row=5 'FNR==row {print $(col); exit}' myfile

 Well yes indeed but it may show you may have missed the
"let's play 'flog' for a change ;-)" part?

 as the OP wanted an undefinite number of solutions
here's another 'flog' way ,-)
---------
awk -v col=3 -v row=5 '
!(FNR-row){
        split($0,v)
        a=v[col]
        print a
        exit
}
FNR-row{
        next
}
---------
0
Reply Loki 11/26/2009 6:36:44 PM

Loki Harfagr wrote:
>Thu, 26 Nov 2009 13:03:22 +0000, Lorenz did cat�:
>[...]
> Well yes indeed but it may show you may have missed the
>"let's play 'flog' for a change ;-)" part?

that may be (due to lack in knowledge about english proverbs)


Lorenz (allready thinking about a getline version 8-)
0
Reply Lorenz 11/27/2009 7:33:13 AM

On Fri, 27 Nov 2009 07:33:13 +0000, Lorenz <lorenznl@yahoo.com> wrote:

>Loki Harfagr wrote:
>>Thu, 26 Nov 2009 13:03:22 +0000, Lorenz did cat :
>>[...]
>> Well yes indeed but it may show you may have missed the
>>"let's play 'flog' for a change ;-)" part?
>
>that may be (due to lack in knowledge about english proverbs)

echo flog |rev

Loki's from .fr

>
>
>Lorenz (allready thinking about a getline version 8-)

The boggle minds...

Grant.
-- 
http://bugsplatter.id.au
0
Reply Grant 11/27/2009 9:12:45 AM

Fri, 27 Nov 2009 20:12:45 +1100, Grant did cat :

> On Fri, 27 Nov 2009 07:33:13 +0000, Lorenz <lorenznl@yahoo.com> wrote:
> 
>>Loki Harfagr wrote:
>>>Thu, 26 Nov 2009 13:03:22 +0000, Lorenz did cat : [...]
>>> Well yes indeed but it may show you may have missed the
>>>"let's play 'flog' for a change ;-)" part?
>>
>>that may be (due to lack in knowledge about english proverbs)
> 
> echo flog |rev
> 
> Loki's from .fr

yup, and that'd make me a frog, maybe the reason why
flog's a natural ;-)
Or maybe Lorenz was thinking to that amelican plovelb?
"Can't tell by looking at a flog how high he will jump." 

>>
>>Lorenz (allready thinking about a getline version 8-)

Noooo, get back!-)

> 
> The boggle minds...

as you requested boggle here's a last and boggling flog,
I promise I won't push any other after that one ;-)
-----------
awk -v col=3 -v row=5 '
{
        row--
        while(++i){
                a[++n]=$(i)
                if($(i)~/[\n]/){
                        row--
                }
                if(!row){
                        while(++i){
                                a[++n]=$(i)
                                if($(i)~/[ \t]/){
                                        col--
                                        if(!col){
                                                for(p=n-1;p;p--){
                                                        printf("%s", a[n-p])
                                                }
                                                printf("%s","\n")
                                                exit
                                        }
                                        n=0
                                        delete(a)
                                }
                        }
                }
        }
}
' RS= FS= yerfile
-----------
0
Reply Loki 11/27/2009 10:24:40 AM

On Nov 27, 6:24=A0pm, Loki Harfagr <l...@thedarkdesign.free.fr.INVALID>
wrote:
> Fri, 27 Nov 2009 20:12:45 +1100, Grant did cat=A0:
>
> > On Fri, 27 Nov 2009 07:33:13 +0000, Lorenz <loren...@yahoo.com> wrote:
>
> >>Loki Harfagr wrote:
> >>>Thu, 26 Nov 2009 13:03:22 +0000, Lorenz did cat=A0: [...]
> >>> Well yes indeed but it may show you may have missed the
> >>>"let's play 'flog' for a change ;-)" part?
>
> >>that may be (due to lack in knowledge about english proverbs)
>
> > echo flog |rev
>
> > Loki's from .fr
>
> yup, and that'd make me a frog, maybe the reason why
> flog's a natural ;-)
> Or maybe Lorenz was thinking to that amelican plovelb?
> "Can't tell by looking at a flog how high he will jump."
>
>
>
> >>Lorenz (allready thinking about a getline version 8-)
>
> Noooo, get back!-)
>
>
>
> > The boggle minds...
>
> as you requested boggle here's a last and boggling flog,
> I promise I won't push any other after that one ;-)
> -----------
> awk -v col=3D3 -v row=3D5 '
> {
> =A0 =A0 =A0 =A0 row--
> =A0 =A0 =A0 =A0 while(++i){
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 a[++n]=3D$(i)
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 if($(i)~/[\n]/){
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 row--
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 }
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 if(!row){
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 while(++i){
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 a[++n]=3D=
$(i)
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 if($(i)~/=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=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 }
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 }
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 }
> =A0 =A0 =A0 =A0 }}
>
> ' RS=3D FS=3D yerfile
> -----------

awk -v LINE=3D2 -v COLUMN=3D5 'NR=3D=3DLINE {print substr($0, COLUMN, 1);
exit}'
0
Reply OX0spy 11/27/2009 1:59:42 PM

On Nov 27, 6:24=A0pm, Loki Harfagr <l...@thedarkdesign.free.fr.INVALID>
wrote:
> Fri, 27 Nov 2009 20:12:45 +1100, Grant did cat=A0:
>
> > On Fri, 27 Nov 2009 07:33:13 +0000, Lorenz <loren...@yahoo.com> wrote:
>
> >>Loki Harfagr wrote:
> >>>Thu, 26 Nov 2009 13:03:22 +0000, Lorenz did cat=A0: [...]
> >>> Well yes indeed but it may show you may have missed the
> >>>"let's play 'flog' for a change ;-)" part?
>
> >>that may be (due to lack in knowledge about english proverbs)
>
> > echo flog |rev
>
> > Loki's from .fr
>
> yup, and that'd make me a frog, maybe the reason why
> flog's a natural ;-)
> Or maybe Lorenz was thinking to that amelican plovelb?
> "Can't tell by looking at a flog how high he will jump."
>
>
>
> >>Lorenz (allready thinking about a getline version 8-)
>
> Noooo, get back!-)
>
>
>
> > The boggle minds...
>
> as you requested boggle here's a last and boggling flog,
> I promise I won't push any other after that one ;-)
> -----------
> awk -v col=3D3 -v row=3D5 '
> {
> =A0 =A0 =A0 =A0 row--
> =A0 =A0 =A0 =A0 while(++i){
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 a[++n]=3D$(i)
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 if($(i)~/[\n]/){
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 row--
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 }
> =A0 =A0 =A0 =A0 =A0 =A0 =A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 =A0 =A0 =
=A0 =A0 }
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =
=A0 =A0 n=3D0
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =
=A0 =A0 delete(a)
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 }
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 }
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 }
> =A0 =A0 =A0 =A0 }}
>
> ' RS=3D FS=3D yerfile
> -----------

awk -v LINE=3D2 -v COLUMN=3D5 'NR=3D=3DLINE {print substr($0, COLUMN, 1);
exit}'
0
Reply OX0spy 11/27/2009 1:59:55 PM

On Nov 27, 6:24=A0pm, Loki Harfagr <l...@thedarkdesign.free.fr.INVALID>
wrote:
> Fri, 27 Nov 2009 20:12:45 +1100, Grant did cat=A0:
>
> > On Fri, 27 Nov 2009 07:33:13 +0000, Lorenz <loren...@yahoo.com> wrote:
>
> >>Loki Harfagr wrote:
> >>>Thu, 26 Nov 2009 13:03:22 +0000, Lorenz did cat=A0: [...]
> >>> Well yes indeed but it may show you may have missed the
> >>>"let's play 'flog' for a change ;-)" part?
>
> >>that may be (due to lack in knowledge about english proverbs)
>
> > echo flog |rev
>
> > Loki's from .fr
>
> yup, and that'd make me a frog, maybe the reason why
> flog's a natural ;-)
> Or maybe Lorenz was thinking to that amelican plovelb?
> "Can't tell by looking at a flog how high he will jump."
>
>
>
> >>Lorenz (allready thinking about a getline version 8-)
>
> Noooo, get back!-)
>
>
>
> > The boggle minds...
>
> as you requested boggle here's a last and boggling flog,
> I promise I won't push any other after that one ;-)
> -----------
> awk -v col=3D3 -v row=3D5 '
> {
> =A0 =A0 =A0 =A0 row--
> =A0 =A0 =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0 =A0 =A0 =A0 =A0 =
=A0 =A0 =A0 =A0 =A0 =A0 printf("%s","\n")
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =
=A0 =A0 =A0 =A0 =A0 =A0 exit
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =
=A0 =A0 }
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =
=A0 =A0 n=3D0
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =
=A0 =A0 delete(a)
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 }
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 }
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 }
> =A0 =A0 =A0 =A0 }}
>
> ' RS=3D FS=3D yerfile
> -----------

awk -v LINE=3D2 -v COLUMN=3D5 'NR=3D=3DLINE {print substr($0, COLUMN, 1)}'
0
Reply OX0spy 11/27/2009 2:01:33 PM

13 Replies
136 Views

(page loaded in 0.193 seconds)

Similiar Articles:

7/23/2012 6:50:57 AM


Reply: