简体   繁体   中英

Using ioreg -l with awk to display the last 6 characters of the serialnumber on a mac

I wish to use a script to grab a certain part of the serial number on a macbook air and use it in combination with another variable to create a Computer name based on the last 6 characters of the computers serial number.

system_profiler SPHardwareDataType | grep 'Serial Number (system)' | awk '{print substr( $0, length($0) - 5 )}'

This code did just that but it doesn't work in single user mode. system_profiler returns a x86PlatformPlugin error and so i tried:

ioreg -l | grep IOPlatformSerialNumber | awk '{print substr( $0, length($0) - 6 )}'

with good result except for the serial ending in a double quote ".

How can i modify the output to get rid of that last character and leave me with the last 6 characters of the Serial Number?

You're very close; just tell awk to take the string of the right length:

ioreg -l | grep IOPlatformSerialNumber | awk '{print substr( $0, length($0) - 6, 6 )}'

From the documentation:

substr(string, start, length)

This returns a length-character-long substring of string, starting at character number start. The first character of a string is character number one.

For example, substr("washington", 5, 3) returns "ing". If length is not present, this function returns the whole suffix of string that begins at character number start. For example, substr("washington", 5) returns "ington". This is also the case if length is greater than the number of characters remaining in the string, counting from character number start.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM