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.
hi, i am trying to run this command on this sample input. i am also expecting the lines in red to be part of the output but for some reason they are not getting outputted:
Code:
[schneidz@hyper ~]$ cat test.lst
L 180 11000000 : chun-li : y
L 180 11000000 : chun-li : n
L 180 11000000 : akuma : y
L 180 11000000 : l33t : y
L 180 11000000 : h4x0rz : n
L 180 11000000 : hello : y
L 180 11000000 : world : n
L 180 11000000 : chun-li : n
[schneidz@hyper ~]$ awk 'index($0,"n") == 66 {print $0}' test.lst
L 180 11000000 : h4x0rz : n
L 180 11000000 : world : n
i think its because there is an n in chun-li but that shouldnt matter since it is not the 66th byte in the record ?
Some of that spacing might have been done with tabs. You can't distinguise that on the screen, and it will affect the character count. See what this reveals:
Code:
tr '\t' '@' <test.lst
Relying on precise character positions in formatted output is often unreliable. Couldn't you just do this instead?
but that shouldnt matter since it is not the 66th byte in the record ?
No. index() returns the index first (leftmost) occurrence of the string. For "chun-li" index() returns something like 28, so the condition is not met and the line is not printed.
Maybe try this:
Code:
$ awk '$7~/n/' in2
L 180 11000000 : chun-li : n
L 180 11000000 : h4x0rz : n
L 180 11000000 : world : n
L 180 11000000 : chun-li : n
... for some reason they are not getting outputted ...
Your awk works correctly on my machine. Perhaps your actual data contains tab characters which look like blanks and that causes confusion. There might be a simpler way to extract the "n" lines. Is that character always the right-most character in each line? Or the right-most non-blank character?
Some of that spacing might have been done with tabs. You can't distinguise that on the screen, and it will affect the character count. See what this reveals:
Code:
tr '\t' '@' <test.lst
Code:
[schneidz@hyper ~]$ tr '\t' '@' <test.lst
L 180 11000000 : chun-li : y
L 180 11000000 : chun-li : n
L 180 11000000 : akuma : y
L 180 11000000 : l33t : y
L 180 11000000 : h4x0rz : n
L 180 11000000 : hello : y
L 180 11000000 : world : n
L 180 11000000 : chun-li : n
Quote:
Originally Posted by rknichols
Relying on precise character positions in formatted output is often unreliable. Couldn't you just do this instead?
Code:
awk '$7 == "n" {print}' test.lst
that would be ideal but some records would have chun li instead of chun-li.
Your awk works correctly on my machine. Perhaps your actual data contains tab characters which look like blanks and that causes confusion. There might be a simpler way to extract the "n" lines. Is that character always the right-most character in each line? Or the right-most non-blank character?
yes this is from db2-sql output, i added : surrounding column-1 to help with post-processing so maybe i can added another string in the sql export, or use awk -F : '{print $3' or maybe n$...
i think its because there is an n in chun-li but that shouldnt matter since it is not the 66th byte in the record ?
index($0, "n") will return the position of the first n,
so index($0, "n") == 66 does not check the 66th char in the line (if it was an n), but the first n (if it was the 66th char)
[schneidz@hyper ~]$ awk 'index($0," n") == 65 {print $0}' test.lst
L 180 11000000 : chun-li : n
L 180 11000000 : h4x0rz : n
L 180 11000000 : world : n
L 180 11000000 : chun li : n
but who knows when i hit a record that reads like chu n-li...
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.