Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I am trying to parse a text file that is generated by an expec script that pulls down some information from a switch.

Here is a sample output:

192      0000.0000.0000        1/g23      Dynamic              
192      0000.0000.0000        ch1        Dynamic              
192      0000.0000.0000        ch1        Dynamic              
192      0000.0000.0000        ch1        Dynamic

The text file has a lot of other junk in it that I am not interested in. I only want the lines that contain the switch number and the port - "1/g23" in the example above.

I did a grep on the file to extract the lines that contain the pattern '/g' and it works great.

Now all I want are the two middle columns. I used awk to print the columns.

0000.0000.0000 1/g23

Now that that is done I want to get ride of the '.' sed worked okay for this

000000000000 1/g23

From here I want to convert only the first column to a MAC address format.

So this
000000000000 1/g23
Becomes this
00:00:00:00:00:00 1/g23

I was able to find a sed command that would accomplish this. But my problem is I only need it done on the first column. I am not quite sure how I can edit only the one column and not lose the association between the MAC and the port.

grep -e "/g" switch6output | awk '{print $2,$3}' | sed 's/\.//g' | sed 's/../&:/g;s/:$//'

I'm sure this is a really stupid way of doing it, but it was the best I could come up with. Is there a more reliable way I can accomplish what I need without losing the MAC and port association?

Thank you.

share|improve this question
add comment

2 Answers

up vote 6 down vote accepted

Just use awk directly:

awk '/\/g/ {
        gsub(/\./, "", $2)
        gsub(/../, "&:", $2)
        sub(/:$/, "", $2) 
        print $2,$3
}'

With this solution you don't need grep nor sed.

share|improve this answer
    
This is awesome! Worked perfectly. Thank you!!!! Sorry to ask another dumb question. does the second gsub with "&:" put the semi-colon in the correct place? I was just trying to figure out the purpose of the "&". and the last sub removes the trailing semi-colon that is left over? –  bourne Jul 26 '12 at 2:25
1  
@bourne you are right - see gnu.org/software/gawk/manual/html_node/String-Functions.html If the special character ‘&’ appears in replacement, it stands for the precise substring that was matched by regexp. –  Ulrich Dangel Jul 26 '12 at 3:07
    
Thank you for the explanation. I am starting to understand it a little more. A friend of mine actually created a little script to test the use of the '&' and it is making it make more sense. Thanks again! –  bourne Jul 26 '12 at 20:08
add comment

Only sed example:

sed -r '\=1/g23=!d;
        s/[^ ]* *//;
        s/ *[^ ]* *$//;
        s/(..)\.(..)/:\1:\2:/g;
        s/::/:/'

If your input contains tabs instead of spaces, you have to adjust the expressions.

share|improve this answer
    
Wow this is great.Thank you very much for the response. I had a little bit of a harder time following this one so I think I might use the awk. But I am going to try and figure out how you did it using this sed expression! Thank you again –  bourne Jul 26 '12 at 2:21
    
@bourne: Here is another one for you to work out :) ... The $x and $d are just to aviod some of the clutter in the regex; and it caters for any port/switch value. If they are hex digits, just change $d to suit... x='(..)'; d='[0-9]+'; sed -rn "/$d\/g$d/{ s/\S+\s+$x$x\.$x$x\.$x$x\s+($d\/g$d).*/\1:\2:\3:\4:\5:\6 \7/p }" file –  Peter.O Jul 26 '12 at 5:45
    
@Peter.O Awesome thank you! I am learning a ton about how sed and awk work from this thread! I will work through both yours and choroba's suggestions so I can understand them better! –  bourne Jul 26 '12 at 19:22
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.