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.
I want to select lines based on their position (i.e. line number).
Sample of File1...
Quote:
Alabama
Alaska
Arizona
Arkansas
California
Colorado
Connecticut
Delaware
Florida
Georgia
Sample of File2...
Quote:
2
3
5
7
Desired output file...
Quote:
Alaska
Arizona
California
Connecticut
This can be done by using "nl" to number the lines in File1 and using "grep -f" but that seems like a brute force technique. Is there a clever and efficient way to do this?
Daniel B. Martin
Click here to see the post LQ members have rated as the most helpful post in this thread.
awk 'FNR == NR{nums[$0];next}FNR in nums' numbers.txt file1.txt
Thank you, grail, for this crisp solution. However, when tested with the sample data shown in the original post the output contained only three state names:
Apparently your samples above contain a trailing blank space at the end of the last line (after Georgia and 7 respectively). Therefore the last number is not treated as such, but as a literal string of two characters.
A slight modification to the grail's code should reveal the arcane:
Code:
awk 'FNR == NR{nums[$0+0];next}FNR in nums' numbers file
the +0 forces $0 to be treated as a number (the blank space is ignored) and the subsequent expression will match as expected.
@colucix: Since the key is a line number, and thus integer, wouldn't nums[int($1)] be cleaner?
BTW, I did not know that array element reference was enough to define it. The GNU Awk manual does mention it -- A reference to an element that does not exist automatically creates that array element, with the null string as its value -- and not as specific to GNU awk. I just hadn't encountered that before. Useful; thanks!
Apparently your samples above contain a trailing blank space at the end of the last line ...
Quite right, and I was unaware of this.
1) Thank you to everyone who contributed ideas, code, corrections, and comments to this thread. It is SOLVED!
2) I use gnome-terminal to browse a file. In this instance I was tripped by trailing blanks which are invisible to the unaided eye. I'd like to use a browser which distinguishes white space with a colored dot or some such. Is there a user setting for gnome-terminal which does this? Is there a different utility which can do this?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.