LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 04-11-2015, 05:04 PM   #1
dr.x
Member
 
Registered: Jan 2013
Posts: 231

Rep: Reputation: Disabled
small help in linux script sed command


Hi Guys ,
i have a code that grap ipv6 address of the server
it seems like below :
ip addr show dev eth0 | sed -e's/^.*inet6 \([^ ]*\)\/.*$/\1/;t;d'

2001:19f0:5c00:9422:5400:ff:fe07:b80d
fe80::5400:ff:fe07:b80d

as we see the result is 2 lines , but what i need is i need only the 1st line with only the 1st 4 octects
i mean i need only
2001:19f0:5c00:9422
the question is , what other shell command can i do so that i get only the 2001:19f0:5c00:9422 only ?


cheers
 
Old 04-11-2015, 06:41 PM   #2
Didier Spaier
LQ Addict
 
Registered: Nov 2008
Location: Paris, France
Distribution: Slint64-15.0
Posts: 11,057

Rep: Reputation: Disabled
I would use a BRE for that:
Code:
bash-4.2# ip addr show dev eth1|grep dynamic|grep -o "\([[:alnum:]]*:\)\{4\}" 
2a01:e34:ec0b:630:
bash-4.2#
Caveat emptor: this solution is not fully portable, as the option -o is a GNU extension.
 
Old 04-11-2015, 07:29 PM   #3
dr.x
Member
 
Registered: Jan 2013
Posts: 231

Original Poster
Rep: Reputation: Disabled
Thnk you so much
 
Old 04-11-2015, 09:28 PM   #4
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,126

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
Similar regex works with sed - no need to shell out twice to grep
Code:
sed -nr '/inet6/ s/.*[[:space:]](([[:alnum:]]*:){4}).*/\1/p'
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
sed command from bash script gushnik1 Linux - Software 6 09-18-2013 02:45 AM
Need help with a sed command in my script papa smurf151 Linux - General 2 03-28-2012 01:51 AM
Need help with a sed command within my script papa smurf151 Linux - Mobile 3 03-23-2012 12:46 PM
Simple shell script - sed command Todd88 Programming 10 07-06-2008 04:33 PM
sed in small BASH script OhLordy Linux - General 1 08-29-2003 11:32 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

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