LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-06-2013, 07:34 PM   #1
itachi8009
LQ Newbie
 
Registered: Aug 2013
Posts: 16

Rep: Reputation: Disabled
Get awk to print relevant fields instead of every field


I have an awk statement that goes through a file to print out certain fields

Code:
awk -F";" -v ip="$AsArg" 'BEGIN {OFS="\t"};{if (ip==$5) dir="OUT" ;else if (ip==$6) dir="IN" };{printf "%3s\t %6s\t %6s\t %4s %8d %7d %10s\n", dir,$3,$4,$7,$8,$9,$1 }'
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?
 
Old 08-06-2013, 08:11 PM   #2
Sydney
Member
 
Registered: Mar 2012
Distribution: Scientific Linux
Posts: 147

Rep: Reputation: 36
Code:
awk -F";" -v 
ip="$AsArg" 
'BEGIN 
    {OFS="\t"};
    {if (ip==$5) 
         dir="OUT";
    else if (ip==$6)
         dir="IN";}

     {printf "%3s\t %6s\t %6s\t %4s %8d %7d %10s\n", dir,$3,$4,$7,$8,$9,$1 }
'
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.

Last edited by Sydney; 08-06-2013 at 08:12 PM.
 
Old 08-06-2013, 08:22 PM   #3
itachi8009
LQ Newbie
 
Registered: Aug 2013
Posts: 16

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Sydney View Post
Code:
awk -F";" -v 
ip="$AsArg" 
'BEGIN 
    {OFS="\t"};
    {if (ip==$5) 
         dir="OUT";
    else if (ip==$6)
         dir="IN";}

     {printf "%3s\t %6s\t %6s\t %4s %8d %7d %10s\n", dir,$3,$4,$7,$8,$9,$1 }
'
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
 
Old 08-06-2013, 08:28 PM   #4
Sydney
Member
 
Registered: Mar 2012
Distribution: Scientific Linux
Posts: 147

Rep: Reputation: 36
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.
 
Old 08-06-2013, 08:32 PM   #5
itachi8009
LQ Newbie
 
Registered: Aug 2013
Posts: 16

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Sydney View Post
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?
 
Old 08-06-2013, 08:32 PM   #6
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
Code:
awk -F";" -v 
ip="$AsArg" 
'BEGIN 
    {OFS="\t"};
    {if (ip==$5) 
         dir="OUT";
    else if (ip==$6)
         dir="IN";}

     /'$AsArg'/{printf "%3s\t %6s\t %6s\t %4s %8d %7d %10s\n", dir,$3,$4,$7,$8,$9,$1 }
'

Last edited by Firerat; 08-06-2013 at 08:39 PM.
 
Old 08-06-2013, 08:42 PM   #7
itachi8009
LQ Newbie
 
Registered: Aug 2013
Posts: 16

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Firerat View Post
Code:
awk -F";" -v 
ip="$AsArg" 
'BEGIN 
    {OFS="\t"};
    {if (ip==$5) 
         dir="OUT";
    else if (ip==$6)
         dir="IN";}

     /'$AsArg'/{printf "%3s\t %6s\t %6s\t %4s %8d %7d %10s\n", dir,$3,$4,$7,$8,$9,$1 }
'
Sorry, this doesn't work, it still gives me everything
 
Old 08-06-2013, 08:49 PM   #8
Sydney
Member
 
Registered: Mar 2012
Distribution: Scientific Linux
Posts: 147

Rep: Reputation: 36
Code:
grep "$AsArg" -f file | 
awk -F";" -v 
ip="$AsArg" 
'BEGIN 
    {OFS="\t"};
    {if (ip==$5) 
         dir="OUT";
    else if (ip==$6)
         dir="IN";}

     /'$AsArg'/{printf "%3s\t %6s\t %6s\t %4s %8d %7d %10s\n", dir,$3,$4,$7,$8,$9,$1 }
'
 
Old 08-06-2013, 08:54 PM   #9
itachi8009
LQ Newbie
 
Registered: Aug 2013
Posts: 16

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Sydney View Post
Code:
grep "$AsArg" -f file | 
awk -F";" -v 
ip="$AsArg" 
'BEGIN 
    {OFS="\t"};
    {if (ip==$5) 
         dir="OUT";
    else if (ip==$6)
         dir="IN";}

     /'$AsArg'/{printf "%3s\t %6s\t %6s\t %4s %8d %7d %10s\n", dir,$3,$4,$7,$8,$9,$1 }
'
So it tries to find the IP from the file and then run it through awk?

Because what its doing instead is trying to read from a file with the name of the IP (grep: 192.168.1.1 No such file or directory)

and without the -f, it has no effect

Last edited by itachi8009; 08-06-2013 at 08:57 PM. Reason: more info
 
Old 08-06-2013, 08:56 PM   #10
Sydney
Member
 
Registered: Mar 2012
Distribution: Scientific Linux
Posts: 147

Rep: Reputation: 36
Quote:
Originally Posted by itachi8009 View Post
So it tries to find the IP from the file and then run it through awk?

Because what its doing instead is trying to read from a file with the name of the IP (grep: 192.168.1.1 No such file or directory)
Sorry drop the -f
 
Old 08-06-2013, 08:59 PM   #11
itachi8009
LQ Newbie
 
Registered: Aug 2013
Posts: 16

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Sydney View Post
Sorry drop the -f
-f won't work either, I'm still getting 100 lines
 
Old 08-06-2013, 09:02 PM   #12
Sydney
Member
 
Registered: Mar 2012
Distribution: Scientific Linux
Posts: 147

Rep: Reputation: 36
so
Code:
grep "192.168.1.1" file | wc -l
returns 100 lines? If so that IP is on every line.

Last edited by Sydney; 08-06-2013 at 09:06 PM.
 
Old 08-06-2013, 09:03 PM   #13
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
Code:
awk -F";" -v ip="$AsArg" 'BEGIN{OFS="\t"};
    {if (ip==$5) 
         dir="OUT";
    else if (ip==$6)
         dir="IN";
    };
/'$AsArg'/{printf "%3s\t %6s\t %6s\t %4s %8d %7d %10s\n", dir,$3,$4,$7,$8,$9,$1 } '

Last edited by Firerat; 08-06-2013 at 09:05 PM. Reason: doublepost
 
Old 08-06-2013, 09:05 PM   #14
itachi8009
LQ Newbie
 
Registered: Aug 2013
Posts: 16

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Sydney View Post
so
Code:
grep "192.168.1.1" file
returns 100 lines? If so that IP is on every line.
Nope, ran grep with wc -l, 11 lines with the IP I am searching for (192.168.1.1 as an example)
 
Old 08-06-2013, 09:08 PM   #15
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
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

Last edited by ntubski; 08-06-2013 at 09:14 PM.
 
1 members found this post helpful.
  


Reply

Tags
awk, bash



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
help on awk print matched field phpshell Programming 10 03-20-2013 10:46 AM
[SOLVED] AWK: print all fields in one column cristalp Programming 3 03-14-2012 11:43 AM
[SOLVED] awk: how to print a field when field position is unknown? elfoozo Programming 12 08-18-2010 03:52 AM
how not to print the 4th field from a text file with six fields livetoday Red Hat 3 10-02-2007 01:19 PM
awk to remove first 3 lines and print remaining $1, $2 fields phyx Linux - General 1 01-10-2007 05:21 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:42 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration