Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

multiple records

1 view
Skip to first unread message

nhgoku...@gmail.com

unread,
Jul 13, 2008, 2:15:55 AM7/13/08
to
Hi,

I have output which looks as follows
abc@xyz blah blah
something something something
othersomething othersomtheing othersomething

and the above 3 line repeated for some 100 times(with different
setting) so i have 300 lines. I want to get print
abc@xyz from 1st lineHi,

I have output which looks as follows
abc@xyz blah blah
something something something
othersomething othersomtheing othersomething

and the above 3 line repeated for some 100 times(with different
setting) so i have 300 lines. I want to get print
abc@xyz from 1st line

Janis Papanagnou

unread,
Jul 13, 2008, 4:12:00 AM7/13/08
to
nhgoku...@gmail.com wrote:
> Hi,
>
> I have output which looks as follows
> abc@xyz blah blah
> something something something
> othersomething othersomtheing othersomething
>
> and the above 3 line repeated for some 100 times(with different
> setting) so i have 300 lines. I want to get print
> abc@xyz from 1st lineHi,

I suppose you meant the first field from each block of data?

awk 'NR%3==1{print $1}' your_data_file


Janis

nhgoku...@gmail.com

unread,
Jul 14, 2008, 1:24:33 AM7/14/08
to
Sorry for confusion let me clear you my problem

I have output which looks as follows
abc@xyz blah blah
something something something
othersomething othersomtheing othersomething

and the above 3 line repeated for some 100 times(with different
setting) so i have 300 lines.

I want to get print
abc@xyz from 1st line

only 3rd field from second and third line. Please help me.

Kenny McCormack

unread,
Jul 14, 2008, 4:21:12 AM7/14/08
to
In article <b2ffa3ee-baf0-4e60...@y38g2000hsy.googlegroups.com>,

Thanks. That just clarifies everything.

Janis Papanagnou

unread,
Jul 14, 2008, 8:06:50 AM7/14/08
to
nhgoku...@gmail.com wrote:
> Sorry for confusion let me clear you my problem

It doesn't clear your posting...

>
> I have output which looks as follows
> abc@xyz blah blah
> something something something
> othersomething othersomtheing othersomething
>
> and the above 3 line repeated for some 100 times(with different
> setting) so i have 300 lines.

...instead, you gave a new requirement here...

> I want to get print
> abc@xyz from 1st line
> only 3rd field from second and third line. Please help me.

Again, I assume you mean "second and third line" from each 3-line
block?

awk '
NR%3==1 {print $1}
NR%3!=1 {print $3}
' your_data_file

I suppose the above is more apparent to understand how it works,
and if it turns out that you want to print any other field you are
able to adjust the code, but here are some variants...

awk 'NR%3==1 {print $1 ; next} {print $3}' your_data_file

awk '{print (NR%3==1)?$1:$3}' your_data_file

Or shall everything (or every record) be in one line?

Janis

nhgoku...@gmail.com

unread,
Jul 14, 2008, 8:25:36 AM7/14/08
to
Thanks janis :) it is solved half of my issue

awk '{print (NR%3==1)?$1:$3}' my_file prints in separate line
am trying to get $1 and $3 in same line and other set follows it like

$1 $3(each set)
$1 $3
.
.
.


Janis Papanagnou

unread,
Jul 14, 2008, 8:44:49 AM7/14/08
to
nhgoku...@gmail.com wrote:
> Thanks janis :) it is solved half of my issue
>
> awk '{print (NR%3==1)?$1:$3}' my_file prints in separate line
> am trying to get $1 and $3 in same line and other set follows it like

Yet another new/changed requirement ???

>
> $1 $3(each set)
> $1 $3
> .
> .
> .
>
>

One last suggestion; type in _exactly_ the first six lines of
sample data and the corresponding desired output. Choose data
that makes it possible to see which input field corresponds to
which output field. Then we will quickly have a solution, be
assured.

Hermann Peifer

unread,
Jul 14, 2008, 2:46:49 PM7/14/08
to

Here a version where $1 and $3 is on the same line.

$ cat output
abc@xyz blah blah
something1 something2 something3
othersomething1 othersomething2 othersomething3
def@xyz blah blah
something11 something22 something33
othersomething11 othersomething22 othersomething33

$ awk 'NR%3==1{f1=$1;next}{print f1,$3}' output
abc@xyz something3
abc@xyz othersomething3
def@xyz something33
def@xyz othersomething33

nhgoku...@gmail.com

unread,
Jul 15, 2008, 3:44:01 AM7/15/08
to
I really thank all you people. This is really a good place for newbies
to grow next level.

now am trying to dig more to get output in the following format

abc@xyz something3
othersomething3
def@xyz something33
othersomething33

Thanks,

On Jul 14, 11:46 pm, Hermann Peifer <pei...@gmx.eu> wrote:

Hermann Peifer

unread,
Jul 15, 2008, 5:54:55 AM7/15/08
to
[Please don't top-post]

On Jul 15, 9:44 am, "nhgokulpra...@gmail.com"
<nhgokulpra...@gmail.com> wrote:

> On Jul 14, 11:46 pm, Hermann Peifer <pei...@gmx.eu> wrote:
>
>
>
> > nhgokulpra...@gmail.com wrote:
> > > Thanks janis :) it is solved half of my issue
>
> > > awk '{print (NR%3==1)?$1:$3}' my_file prints in separate line
> > > am trying to get $1 and $3 in same line and other set follows it like
>
> > > $1 $3(each set)
> > > $1 $3
> > > .
> > > .
> > > .
>
> > Here a version where $1 and $3 is on the same line.
>
> > $ cat output
> > abc@xyz blah blah
> > something1 something2 something3
> > othersomething1 othersomething2 othersomething3
> > def@xyz blah blah
> > something11 something22 something33
> > othersomething11 othersomething22 othersomething33
>
> > $ awk 'NR%3==1{f1=$1;next}{print f1,$3}' output
> > abc@xyz something3
> > abc@xyz othersomething3
> > def@xyz something33

> > def@xyz othersomething33- Hide quoted text -
>

> I really thank all you people. This is really a good place for newbies
> to grow next level.
>
> now am trying to dig more to get output in the following format
>
> abc@xyz something3
> othersomething3
> def@xyz something33
> othersomething33
>

This Gawk 3-liner:

gawk '
NR%3==1 {f1=$1 ; next}
NR%3 {print f1,$3 ; next}
{printf "%*s %s\n", length(f1),"",$3} ' output

... prints this:

jmh

unread,
Jul 15, 2008, 6:26:22 PM7/15/08
to
On 2008-07-15, Hermann Peifer <pei...@gmx.net> wrote:
> gawk '
> NR%3==1 {f1=$1 ; next}
> NR%3 {print f1,$3 ; next}
> {printf "%*s %s\n", length(f1),"",$3} ' output

I understand the output but am not sure
i understand why the NR%3 in the
second line above is there.

jmh

Kenny McCormack

unread,
Jul 15, 2008, 8:15:48 PM7/15/08
to
In article <slrng7q9gh...@localhost.my.domain>,

The first line selects lines congruent to 1 mod 3 (1,4,7, etc), then skips out.
The second line selects lines congruent to 2 mod 3 (2,5,8, etc), then skips out.
The last line selects lines congruent to 0 mod 3 (3,6,9, etc).

Hermann Peifer

unread,
Jul 16, 2008, 2:40:23 AM7/16/08
to

Just to add that one could write the second rule more explicit, as follows:

NR%3==2 {print f1,$3 ; next}

But the "==2" part is in fact not needed, given the preceding rule.

Hermann

0 new messages