vim use the Find command, all the decimal conversion of hexadecimal digits

Billion, Preface

  In doing program development, usually somewhat ID will each seed-digit ID by splicing, as shown below:

Role ID = server ID 16 bits left + players subscript 
the UserID = ServerID << 16 + UserIndex

  If this ID is printed inside the log is displayed as a decimal, to match the same server ID of the players it will be more difficult.

  This time we can use the rich VIM function, the numbers in server log files into hexadecimal, and thus can be very easily match the same server ID of the player.

First, the operating procedures

  1. Open the file with vim

[WARN | 2020-01-06 15:02:11,974] | UserID[655471] User Online.
[WARN | 2020-01-06 15:02:11,974] | UserID[655415] User Offline.
[WARN | 2020-01-06 15:02:11,974] | UserID[655471] Create User Success.
[WARN | 2020-01-06 15:02:11,974] | UserID[659811] User Offline
[WARN | 2020-01-06 15:02:11,974] | UserID[918509] User Offline
[WARN | 2020-01-06 15:02:11,974] | UserID[720895] User Offline
[WARN | 2020-01-06 15:02:11,974] | UserID[720897] User Offline

  2. Enter the following:

:%s/\d\+/\=printf("%X",submatch(0))/g

  3. The final results are shown below:

[WARN | 7E4-1-6 F:2:B,3CE] | UserID[A006F] User Online.
[WARN | 7E4-1-6 F:2:B,3CE] | UserID[A0037] User Offline.
[WARN | 7E4-1-6 F:2:B,3CE] | UserID[A006F] Create User Success.
[WARN | 7E4-1-6 F:2:B,3CE] | UserID[A1163] User Offline
[WARN | 7E4-1-6 F:2:B,3CE] | UserID[E03ED] User Offline
[WARN | 7E4-1-6 F:2:B,3CE] | UserID[AFFFF] User Offline
[WARN | 7E4-1-6 F:2:B,3CE] | UserID[B0001] User Offline

  4. At this point, the server ID is looking for 10 (that is, 16 hex A) of the players very easily.

Second, the command Detailed

  1 "\ d \ +":. Matches one or more digits, vim help Reference: ": help \ d" and ": help \ d"

  2. "printf": the specified output format, vim help Reference: ": help printf"

  3. "submatch": Low return text matching the N, if N is 0, returns the entire matched text. vim help Reference: ": help submatch"

  Therefore, the above command: ":% s / \ d \ + / \ = printf ("% X ", submatch (0)) / g" can be broken down as follows:

  

:% S / \ D \ + / \ = the printf ( " % X- " , submatch ( 0 )) /    G 
global matching matching matching digital content into hexadecimal, replace all 
namely: the digital conversion match to ten hex, global matching replace all

 

Guess you like

Origin www.cnblogs.com/minglee/p/12160027.html