LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 04-22-2013, 02:09 PM   #1
DComeaux1861
LQ Newbie
 
Registered: Jan 2013
Posts: 2

Rep: Reputation: Disabled
Yet another awk question.


Can anyone explain why when i run awk below I get the entire 1st line? Ultimately I'm going to have a list of reverse DNS looked up items that I'm going to use to identify what's in a switch.

Sorry if this should go elsewhere. I know this is something obvious... So obvious I think the newbie forum is appropriate.

For you newbies, this is more on the business side of linux. i have a list of mac addresses with IP addresses that I just wanted to reverse look-up. Easy-peasy once you know how to use pipes.

[root@vnb01 arp]# head 10.15.61.0.macs

10.15.61.1,00:00:0C:07:AC:0A
10.15.61.3,00:E0:86:02D:C8
10.15.61.4,00:11:43:88:8E:88
10.15.61.5,00:A0:A5:4B:18:0C
10.15.61.6,00:A0:98:11:5C:08
10.15.61.7,00:05:1E:02:20:74
10.15.61.8,00:60:2E:01:E8:C3
10.15.61.9,00:60:2E:01:E8:B0
10.15.61.10,00:0F:1F:F9:2F:F3
[root@vnb01 arp]# cat 10.15.61.0.macs|awk '/,/ {FS=",";print $1}'|head
10.15.61.1,00:00:0C:07:AC:0A
10.15.61.3
10.15.61.4
10.15.61.5
10.15.61.6
10.15.61.7
10.15.61.8
10.15.61.9
10.15.61.10
10.15.61.11
[root@vnb01 arp]#
 
Old 04-22-2013, 02:20 PM   #2
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,186

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
Sure. You've defined FS after $0 has been set to the first line.

Try
Code:
awk -F ',' '/,/{print $1}' 10.15.61.0.macs|head
 
1 members found this post helpful.
Old 04-22-2013, 02:45 PM   #3
DComeaux1861
LQ Newbie
 
Registered: Jan 2013
Posts: 2

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by PTrenholme View Post
Sure. You've defined FS after $0 has been set to the first line.

Try
Code:
awk -F ',' '/,/{print $1}' 10.15.61.0.macs|head


Proof kiddies that you never know it all and you never will. 8) But oh, what a noble goal. Its like academic ADD.

Well done, Trenholme.

So apparently $0, $1, etc. get set sooner than I thought.
 
Old 04-22-2013, 02:59 PM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,243

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
I am guessing there are other lines in the file that do not have the separator in them otherwise the /,/ is pointless.

I would add though that you could also dispense with head as well:
Code:
awk -F, 'NR>10{exit}/,/{print $1}' 10.15.61.0.macs
 
Old 04-23-2013, 11:49 AM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
I'd also add that you could consider a few other options as well.

Code:
sed -n '1,10 { s/,.*//p }; 11 Q' 10.15.61.0.macs

cut -d',' -f 1 10.15.61.0.macs | head

n=1
while IFS=, read -r line _ ; do
    (( n++ <=10 )) && echo "$line" || break
done <10.15.61.0.macs
The last one could even be used to set the numbers into an array, if you intend to do further processing on them afterwards. Just change the middle line to:

Code:
(( n++ <=5 )) && array+=( "$line" ) || break
 
  


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
[SOLVED] awk question ghantauke Linux - Newbie 7 11-15-2010 04:54 PM
awk question on handling *.CSV "text fields" in awk jschiwal Programming 8 05-27-2010 07:23 AM
awk question cmontr Programming 12 11-19-2007 12:35 PM
awk question denalitastic Linux - Newbie 1 06-07-2005 11:42 AM
awk question dilberim82 Programming 2 10-01-2003 08:12 PM


All times are GMT -5. The time now is 06:48 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