PDA

View Full Version : string handling



jpjohnj
October 28th, 2011, 11:09 AM
i need to get the mac address for interface...

( with only using "sed")


actually i got this ...


ip link show dev eth2|sed '$!d'


and the out put was ...


link/ether 00:12:0e:00:00:00 brd ff:Ff:Ff:ff:ff:Ff


in above i only need "00:12:0e:00:00:00" .


help me to do that with sed.... ( not with awk,cut,tail,grep)

nothingspecial
October 28th, 2011, 11:20 AM
Hi jpjohnj,

Is this homework?

jpjohnj
October 28th, 2011, 11:28 AM
not home work .. i need to use that in my program, my device kernel does not support gerp ,cut,tail. so that only i need a help..

Lars Noodén
October 28th, 2011, 11:48 AM
Awk should be on your machine.



ip link show dev eth2 | awk '/link\/ether/ { print $2 }'

jpjohnj
October 28th, 2011, 11:55 AM
only string handling function it has was sed ... please help me to do that....


( help with other than awk,cut,grep,tail,head)

Lars Noodén
October 28th, 2011, 12:11 PM
only string handling function it has was sed ... please help me to do that....


( help with other than awk,cut,grep,tail,head)

It may not be possible using only sed. That's why I proposed awk.

If you can merge the lines into one, it might be possible to then combine several sed actions. First merge all the lines into one, then erase everything before the mac address, then erase everything after the mac address.



ip link show dev eth2 | sed -e 's/foo//;s/^.*ether[ ]*//;s/[ ]*brd.*$//'


The part 'foo' is what I haven't figured out. That should be the part that eliminates the end of line characters.

Lars Noodén
October 28th, 2011, 12:32 PM
Try this:



ip link show dev eth2 | sed -e 'N;s/\n//g;s/^.*ether[ ]*//;s/[ ]*brd.*$//'


N = read an extra line, so this should work if ip outputs exactly two lines, no more, no less

jpjohnj
October 28th, 2011, 12:42 PM
thank for your help..


could you explain that to me in a clear way...


sed -e 's/foo//;s/^.*ether[ ]*//;s/[ ]*brd.*$//'

and


sed -e 'N;s/\n//g;s/^.*ether[ ]*//;s/[ ]*brd.*$//'

Lars Noodén
October 28th, 2011, 12:42 PM
This should work with an arbitrary number of lines but assuming only one has 'ether' in it:



ip link show dev eth2 | sed -n '/ether/p' | sed -e 's/^.*ether[ ]*//;s/[ ]*brd.*$//'

Lars Noodén
October 28th, 2011, 12:46 PM
thank for your help..


could you explain that to me in a clear way...


sed -e 's/foo//;s/^.*ether[ ]*//;s/[ ]*brd.*$//'

and


sed -e 'N;s/\n//g;s/^.*ether[ ]*//;s/[ ]*brd.*$//'

You have multiple substitutions, each one is s///

The first part is the regex you find, the second is what your replace, s/find/replace/

[] means find any characters that match the ones between the brackets, in this case as space

* means match zero or more characters, when combined with the brackets containing a space, it means match zero or more spaces

ether and brd are the strings ether and brd

^ means start matching from the beginning of the line
$ means match until the very end of the line

Normally sed only matches one line at at time, but N means read in an extra line first.


Does that help?

http://en.wikipedia.org/wiki/Regular_expression

jpjohnj
October 28th, 2011, 12:49 PM
thank you...

mörgæs
October 28th, 2011, 12:53 PM
Moved to Programming Talk.

If the problem is solved, please mark the thread so using 'thread tools'.

Lars Noodén
October 28th, 2011, 01:12 PM
And one last one for the archive.


ip link show dev eth2 | \
sed ':a;N;$!ba;s/\n//g' (http://linux.dsplabs.com.au/rmnl-remove-new-line-characters-tr-awk-perl-sed-c-cpp-bash-python-xargs-ghc-ghci-haskell-sam-ssam-p65/) | \
sed -e 's/^.*ether[ \t]*//;s/[ \t]*brd.*$//'

I hope I can find it easily next time I need it. :)