LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Yet another awk question. (https://www.linuxquestions.org/questions/linux-newbie-8/yet-another-awk-question-4175459160/)

DComeaux1861 04-22-2013 01:09 PM

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:02:DD: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]#

PTrenholme 04-22-2013 01:20 PM

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

DComeaux1861 04-22-2013 01:45 PM

Quote:

Originally Posted by PTrenholme (Post 4936656)
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.

grail 04-22-2013 01:59 PM

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

David the H. 04-23-2013 10:49 AM

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


All times are GMT -5. The time now is 01:44 PM.