LinuxQuestions.org
Visit Jeremy's Blog.
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-27-2013, 09:02 PM   #1
psycroptic
Member
 
Registered: Aug 2011
Location: USA
Distribution: ArchLinux - 3.0 kernel
Posts: 349

Rep: Reputation: Disabled
awk question - FS/RS works in 1 instance, doesn't in another


i am woefully unfamiliar with awk. i am wanting to return whatever is in between 2 specific words in a string. I have this:

Code:
ip addr show external | grep inet
which gives me this:

Code:
    inet xx.xx.xx.xx/22 brd 255.255.255.255 scope global external
I want to return that address between "inet" and "brd". So i do this:

Code:
ip addr show external | grep inet | awk 'BEGIN{FS="inet";RS="brd"}/</{print $2}'
Which i would think should work, as I've used that RS= FS= thing before with nearly-identical syntax and expected behavior, but here I get no output at all.

Ideas?
 
Old 08-27-2013, 10:24 PM   #2
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
looks like you are over thinking it..

Code:
ip addr show external | awk '/inet/{print $2}'


https://www.gnu.org/software/gawk/manual/

Edit:

actually, if you have inet6 then..
Code:
awk '$1 == "inet" {print $2}'
# or
awk '/inet /{print $2}'

Last edited by Firerat; 08-27-2013 at 10:34 PM.
 
Old 08-28-2013, 08:53 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Personally I would have never expected the awk example to work as the regex you are using does not exist, ie. '<' is no where in the line shown
 
Old 08-28-2013, 10:10 AM   #4
psycroptic
Member
 
Registered: Aug 2011
Location: USA
Distribution: ArchLinux - 3.0 kernel
Posts: 349

Original Poster
Rep: Reputation: Disabled
cool thanks, the thing firerat posted worked. as i said, i'm not too familiar with awk.

aprreciate it
 
  


Reply



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
slackpkg doesn't works (problems with awk) sparc86 Slackware 8 04-11-2007 11:08 AM
Mounting works, playing music works, reading tags doesn't Celettu Linux - Newbie 7 08-23-2006 12:27 PM
regexp question: first instance for each line buldir Programming 6 03-20-2006 01:20 PM
Firefox - HTTPS works, but HTTP doesn't, any ideas? (Newbie question) RichardDarling Linux - Networking 2 11-03-2005 08:23 AM
Echo /devPrinting doesn't work, echo /usb/lp0 works, Testpage works, Printing doesn't Hegemon Linux - General 3 08-15-2002 01:13 PM

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

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