awk getline to display first three lines in file

asad a.alii85 at gmail.com
Sat Jan 2 09:03:56 UTC 2016


On Thu, Dec 31, 2015 at 4:06 AM, MR <mrzenwiz at gmail.com> wrote:

>
>
> On Wed, Dec 30, 2015 at 11:03 AM, Normand Marion <normand.marion at gmail.com
> > wrote:
>
>> 2015-12-29 10:12 GMT-05:00 asad <a.alii85 at gmail.com>:
>>
>>> I'm using
>>>
>>>     awk 'BEGIN {while((getline gf < "awk.script") > 0) {print gf}}'
>>>
>>> to read me first 3 three lines of the pattern. I can do with head -3 .
>>> But I don't want to since, I have to switch lines between two files.
>>>
>>> Suppose awk.script contains
>>>
>>>
>>>     line 1
>>>     line 2
>>>     line 3 <------- until here
>>>     line 4
>>>
>>> I have tried updating the original cmd with this
>>>
>>>      awk 'BEGIN {while((getline gf < "awk.script") > 0) && f{print
>>> $gf;f=0};NR<=3{f=1}}'
>>>
>>> the code is influenced from the examples give on link:-
>>>
>>> http://stackoverflow.com/questions/17908555/printing-with-sed-or-awk-a-line-following-a-matching-pattern
>>>
>>> error i'm getting is
>>>
>>>     awk: cmd. line:1: BEGIN {while((getline gf < "awk.script") > 0)
>>> f{print $gf;f=0};NR<=3{f=1}}
>>>     awk: cmd. line:1:                                                ^
>>> syntax error
>>>     awk: cmd. line:1: BEGIN {while((getline gf < "awk.script") > 0)
>>> f{print $gf;f=0};NR<=3{f=1}}
>>>     awk: cmd. line:1:
>>>
>>> How can it be done? thanks
>>>
>>> awk '{ if ( NR <= 3 ) print $0 }' your_file
>
> You can also do this to save time in case the file is large:
>
> awk '{print $0;} NR >= 3 { exit; }' your_file
>
> thanks but this only caters for single file not two?.

>
> --
> ubuntu-users mailing list
> ubuntu-users at lists.ubuntu.com
> Modify settings or unsubscribe at:
> https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.ubuntu.com/archives/ubuntu-users/attachments/20160102/ce17a9e8/attachment.html>


More information about the ubuntu-users mailing list