Grep /Awk letters X - X in every line and print it as a mac address


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Grep /Awk letters X - X in every line and print it as a mac address
# 1  
Old 11-15-2011
Debian Grep /Awk letters X - X in every line and print it as a mac address

hey
i m kinda new to this so i will appreciate any help
, i have this list of values:
Code:
pwwn = 0x50012482009cd7a7 nwwn=0x50012482009cd7a6 port_id = 0x280200
pwwn = 0x5001248201bcd7a7 nwwn=0x5001248201bcd7a6 port_id = 0x280300
pwwn = 0x50012482009c51ad nwwn=0x50012482009c51ac port_id = 0x280400
pwwn = 0x5001248201bc51ad nwwn=0x5001248201bc51ac port_id = 0x280500
pwwn = 0x5001248045c13b33 nwwn=0x5001248045c13b32 port_id = 0x280600
pwwn = 0x5001248145c13b33 nwwn=0x5001248145c13b32 port_id = 0x280700
pwwn = 0x21000024ff303570 nwwn=0x20000024ff303570 port_id = 0x281000
pwwn = 0x21000024ff303571 nwwn=0x20000024ff303571 port_id = 0x281100

i need to grep only this section "pwwn = 0x50012482009cd7a7" from every line
and then print it as a mac address : 50:01:24:82:00:9c:d7:a7 ( without the "pwwn = 0x")

at first i tried to grep only the 25 first letters , but the file contains llines that are not of the same structure so that failed.
Please advise
thank u all.


Moderator's Comments:
Mod Comment Please use code tags!

Last edited by zaxxon; 11-15-2011 at 06:17 AM.. Reason: code tags, see PM
# 2  
Old 11-15-2011
Code:
perl -pe'
  s/pwwn = 0x(\S+).*/($x=$1)=~s|..(?!\z)|$&:|g;$x/e
  ' infile

This User Gave Thanks to radoulov For This Post:
# 3  
Old 11-15-2011
Code:
echo "pwwn = 0x50012482009cd7a7 nwwn=0x50012482009cd7a6 port_id = 0x280200" | awk '{ print $3 }'| sed -e 's/^0x//g' -e 's/../&:/g' -e 's/:$//'

This User Gave Thanks to maskofzorro For This Post:
# 4  
Old 11-15-2011
thanx

Quote:
Originally Posted by radoulov
Code:
perl -pe'
  s/pwwn = 0x(\S+).*/($x=$1)=~s|..(?!\z)|$&:|g;$x/e
  ' infile

thank u for that , i wanted to know if there is a way to run it only on exact match to "pwwn = 0x"
# 5  
Old 11-15-2011
Sure:

Code:
perl -ne'
  s/pwwn = 0x(\S+).*/($x=$1)=~s|..(?!\z)|$&:|g;$x/e
    and print
  ' infile

This User Gave Thanks to radoulov For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. IP Networking

MAC Address - Four Interfaces with the same MAC Address

four interfaces with ifconfig all interfaces have the same mac. If is not set for unique. but it still works. what difference does it make to have all macs the same or different? (4 Replies)
Discussion started by: rrodgers
4 Replies

2. Shell Programming and Scripting

Grep echo awk print all output on one line

Hello, I've been trying to find the answer to this with Google and trying to browse the forums, but I haven't been able to come up with anything. If this has already been answered, please link me to the thread as I can't find it. I've been asked to write a script that pulls a list of our CPE... (51 Replies)
Discussion started by: rwalker
51 Replies

3. Shell Programming and Scripting

Print hex Ip address in decimal format inside awk script

Hi to all, May someone help me with the following. The awk script below is part of a bigger awk script, and this part attempts to print an Ip address that is in hex format in decimal format. I'm trying with following code but I'm getting 0.0.0.0 and the correct answer is 192.168.140.100 ... (9 Replies)
Discussion started by: Ophiuchus
9 Replies

4. Shell Programming and Scripting

GREP mac address

Hi, mac.txt My mac address is <Mac Address>. How can i replace <Mac Address> with the actual of my computer? I try to GREP command as below but i am unable to grep it to replace just <Mac Address>. ifconfig eth0 | grep -o -E '(]{1,2}:){5}]{1,2}' Million in Advance. Please use... (7 Replies)
Discussion started by: derrickyee81
7 Replies

5. Shell Programming and Scripting

Use less pipe for grep or awk sed to print the line not include xx yy zz

cat file |grep -v "xx" | grep -v "yy" |grep -v "zz" (3 Replies)
Discussion started by: yanglei_fage
3 Replies

6. Shell Programming and Scripting

Grep/Awk on 1st 2 Letters in 2nd Column of File

Hi everyone. I need to change a script (ksh) so that it will grep on the 1st 2 letters in the second column of a 5 column file such as this one: 192.168.1.1 CAXY0_123 10ABFL000001 # Comment 192.168.1.2 CAYZ0_123 10ABTX000002 # Comment 192.168.2.1 FLXY0_123 11ABCA000001 ... (4 Replies)
Discussion started by: TheNovice
4 Replies

7. Shell Programming and Scripting

grep grab 19 letters from now or a full line

Hi, I have a file like this >hg19_chr1_123_456_+ asndbansbdahsjdbfsjhfghjdsghjdghjdjhdghjjdkhfsdkjfhdsjkdkjghkjdhgfjkhjfkf hasjgdhjsgfhjdsgfdsgfjhdgjhdjhdhjdfhjdfjgfdfbdghjbfjksdhfjsfdghjgdhjgfdjhgd jhgdfj >hg19_chr1_123_456_-... (5 Replies)
Discussion started by: jacobs.smith
5 Replies

8. Shell Programming and Scripting

Awk+Grep Input file needs to match a column and print the entire line

I'm having problems since few days ago, and i'm not able to make it works with a simple awk+grep script (or other way to do this). For example, i have a input file1.txt: cat inputfile1.txt 218299910417 1172051195 1172070231 1172073514 1183135117 1183135118 1183135119 1281440202 ... (3 Replies)
Discussion started by: poliver
3 Replies

9. Shell Programming and Scripting

awk add Mac Address to new File

I have file one containing Mac Addresses minus colon. I would like to add them to a different file with columns seperated by commas. Such as File 1 0000aa1122 0000aa1123 0000aa1126 Output File Name, MacAddress,Date Something, 0000aa1122,Something Something, 0000aa1123,Something... (6 Replies)
Discussion started by: dolacap
6 Replies

10. IP Networking

Mac address - what is line command to find and active new card

How do you find mac address and active new card. (1 Reply)
Discussion started by: wmetz
1 Replies
Login or Register to Ask a Question