[SOLVED] Get awk to print relevant fields instead of every field
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.
The if statement is suppose to decide the direction and the rest of the fields are from the file itself, however, when I run it, it gives me every line, how do I get awk to print the lines that match the ip variable, instead of every line in the file?
I think you would be happier doing this with grep but dir="IN"}; should have the ; moved. You may also want to provide some input and what you hope the output to be.
I think you would be happier doing this with grep but dir="IN"}; should have the ; moved. You may also want to provide some input and what you hope the output to be.
Here's some of the output - 100 lines
Code:
OUT virbr0 eth1 UDP 5534 13183 10:49:18 24 Apr
OUT virbr0 TCP 17024 19068 14:10:21 14 Feb
OUT em2 em0 ICMP 0 0 04:48:16 23 Aug
OUT em0 eth1 UDP 19986 24286 15:23:17 03 Apr
OUT em2 eth0 TCP 10064 2443 04:11:51 15 Nov
however when I search through the file for the IP I used, it comes up with 11 lines
The file it is reading from has 100 lines total, so it is disregarding the matching IP and just assigning everything with IN/OUT
Okay so on your if else if you have no match for what to do if no match is made, since dir is not changed from the last match I think you will see it again.
Okay so on your if else if you have no match for what to do if no match is made, since dir is not changed from the last match I think you will see it again.
Sorry, but I have trouble understanding what you are saying, how do I make it so that it only gives me the entries that match the IP?
How could any of the solutions posted* even run? Aren't there some backslashes missing? I think this should work (but it would be helpful to have sample input to go with the sample output):
Code:
awk -F";" -v ip="$AsArg" '
BEGIN { OFS="\t"; }
{
if (ip==$5)
dir="OUT";
else if (ip==$6)
dir="IN";
else
dir=0;
}
dir {printf "%3s\t %6s\t %6s\t %4s %8d %7d %10s\n", dir,$3,$4,$7,$8,$9,$1 }'
* There was some overlap in posting times, this refers only to posts before #13
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.