ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
So I was working on a solution for a different problem when I came across an unusual result whilst using awk.
Input data:
Code:
1 , 2
3 , 4
5 , 6
7 , 8
9 , 10
Now the data is trivial and not at all important, but the format will help demonstrate the issue. The first objective is to print each number on its own line using awk.
Trivially this can be solved with (of course there are other ways):
Code:
$ awk '1' RS'[,\n ]+' file
1
2
3
4
5
6
7
8
9
10
So far so good. Now we want to print every second entry from the list.
Again there are alternate solutions, but my quirk comes from using getline:
Code:
$ awk 'getline' RS'[,\n ]+' file
2
4
6
8
I would have expected that the number 10 should also have been displayed for after the number 8 is returned the 9th line of our output, based on the first script showing that by
setting RS we should return 10 lines, is read and then getline should retrieve the number 10 and display it.
If someone would explain where my thinking has gone awry I would be very appreciative
Thanks for confirming it is gawk specific. So I wonder if this is a bug I should raise or am I misunderstanding how getline is functioning when RS is a computed regex using a modifier like plus??
Here is more proof that RS is working as expected:
So I wonder if this is a bug I should raise or am I misunderstanding how getline is functioning when RS is a computed regex using a modifier like plus??
It looks like a bug to me. There is some weird dependency on the exact form of regex:
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.