LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-06-2014, 06:12 PM   #1
battles
Member
 
Registered: Apr 2014
Distribution: Debian GNU/Linux 7.5 (wheezy)
Posts: 231

Rep: Reputation: 5
awk problem


This works:

awk '$2=="2" { print }' <f1> >> <f2>


Can't get this to work:

pkts=2
awk '$2==$pkts { print }' <f1> >> <f2>

Tried "$pkts" also.
 
Old 12-06-2014, 06:27 PM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 14,843

Rep: Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823
You need to pass the variable in on the command invocation - see "Command-Line Options" (and following section) in the manual. Better description than the manpage.
 
Old 12-06-2014, 06:33 PM   #3
battles
Member
 
Registered: Apr 2014
Distribution: Debian GNU/Linux 7.5 (wheezy)
Posts: 231

Original Poster
Rep: Reputation: 5
This works without the command invocation, so it would seem that 'awk '$2==$pkts { print }' <f1> >> <f2>" would work also.
 
Old 12-06-2014, 06:33 PM   #4
bigrigdriver
LQ Addict
 
Registered: Jul 2002
Location: East Centra Illinois, USA
Distribution: Debian Jessie 8.4
Posts: 5,873

Rep: Reputation: 348Reputation: 348Reputation: 348Reputation: 348
Find on the web a copy of The GNU Awk User's Guide. Look at Section 2.2 Command-Line Options. Focus on the -v option for variable assignment.
 
Old 12-07-2014, 09:02 AM   #5
battles
Member
 
Registered: Apr 2014
Distribution: Debian GNU/Linux 7.5 (wheezy)
Posts: 231

Original Poster
Rep: Reputation: 5
Code:
I am not having any luck with this.  Can someone give me an example of how to extract the records in my iptables that have a 1 in the second column?

118      7   280 DROP       all  --  *      *       1.161.0.0         0.0.0.0/0            /* CN  BOA    1  Nov 13 2014 01:16:17 AM */
253      1     0 DROP       all  --  *      *       1.169.0.0         0.0.0.0/0            /* TW  BOA Dec 06 2014 08:24:41 PM */
132      5   200 DROP       all  --  *      *       1.171.0.0         0.0.0.0/0            /* TW  BOA    1  Nov 15 2014 01:29:19 PM */
185      1   240 DROP       all  --  *      *       2.50.0.0          0.0.0.0/0            /* AE SSH Nov 25 2014 06:43:19 PM */
116      7   280 DROP       all  --  *      *       5.196.0.0         0.0.0.0/0            /* CN  BOA    1  Nov 12 2014 08:23:52 PM */
252      0     0 DROP       all  --  *      *       5.39.0.0          0.0.0.0/0            /* NL  BOA Dec 06 2014 08:03:06 PM */

Result of extraction:
253      1     0 DROP       all  --  *      *       1.169.0.0         0.0.0.0/0            /* TW  BOA Dec 06 2014 08:24:41 PM */
185      1   240 DROP       all  --  *      *       2.50.0.0          0.0.0.0/0            /* AE SSH Nov 25 2014 06:43:19 PM */
Thanks.

Last edited by battles; 12-07-2014 at 11:05 AM.
 
Old 12-07-2014, 12:06 PM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,255

Rep: Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686
So you have marked this as SOLVED ... would you like to share your solutions so others may benefit?
 
Old 12-07-2014, 01:39 PM   #7
battles
Member
 
Registered: Apr 2014
Distribution: Debian GNU/Linux 7.5 (wheezy)
Posts: 231

Original Poster
Rep: Reputation: 5
Quote:
Originally Posted by grail View Post
So you have marked this as SOLVED ... would you like to share your solutions so others may benefit?
Sorry about that. I thought that the former explanations solved it for me, but I couldn't figure it out. I didn't see that you could un-solve a solved, it is unsolved now.
I do always try to post my solutions to help others who might need the same answer. If someone tells me how to do this or I am ever able to solve it, I will post.
 
Old 12-07-2014, 01:48 PM   #8
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,129

Rep: Reputation: 2271Reputation: 2271Reputation: 2271Reputation: 2271Reputation: 2271Reputation: 2271Reputation: 2271Reputation: 2271Reputation: 2271Reputation: 2271Reputation: 2271
You know next comes the question: what have you really tried? What do you mean by former explanations?
You can pass variables to awk (as it was mentioned in #4:
awk -v awk_var="$shell_var" '$2 == awk_var { print }'
should work, but { print } actually can be omitted.
Remember shell variables are always beginning with a $ sign, but in awk there is no $.

also to the question in #5 you can try:
awk '$2 == 1'
 
Old 12-07-2014, 02:02 PM   #9
battles
Member
 
Registered: Apr 2014
Distribution: Debian GNU/Linux 7.5 (wheezy)
Posts: 231

Original Poster
Rep: Reputation: 5
AH! Thank you very much! I tried something like awk -v awk_var="$shell_var" '$2 == awk_var { print }' , but didn't get it formatted right.
Works as needed.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
turning awk command line entries into awk scripts tabbyagirl Linux - Newbie 5 08-22-2013 01:46 AM
[SOLVED]Wierd AWK behavior / AWK not reading first line. Involar Linux - Newbie 9 11-28-2012 11:53 AM
awk error awk: line 2: missing } near end of file boscop Linux - Networking 2 04-08-2012 11:49 AM
[SOLVED] call awk from bash script behaves differently to awk from CLI = missing newlines titanium_geek Programming 4 05-26-2011 10:06 PM
AWK problem pppaaarrrkkk Programming 10 02-29-2008 08:29 AM


All times are GMT -5. The time now is 06: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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration