shell split string

Article directory

shell split string

insert image description here

Recently, newcomers have been asking how to parse the log data in the business, so here is a brief record. Hope to give you help!

In business, especially in scenarios with long links, log data will be spliced ​​and aggregated into Log and Trace in a certain format, but Metrics rarely adopts this method. When R&D personnel process the log, they need to clarify its internal structure, split the data, reduce the large to small, and restore the original data. So how to split it? It may be the confusion of some students.

shell

Some students may think that it is not a problem after reading the above scene. In various development languages, Json and built-in or package functions for splitting and splicing with certain characters are included, which is very convenient. After writing code, the original data will come out after a run.

Yes, this is also one of the ways used in normal production environments.

However, in a non-production environment, when it is necessary to retrieve, observe and analyze the original data, such a method is a bit cumbersome, especially when the splicing structure is relatively complex. A simple shell script is provided here to split a string by input. More concise, flexible and lightweight.

Complex character strings: After concatenating the second/third-level fields with \t as the delimiter, the first-level fields use 001 as the delimiter...

{s//sep/}

The content of the script split.sh is as follows:

#!/bin/bash

s=$1
sep=$2

function deal(){
    
    
  arr=(${
    
    s//$sep/ })
  echo ${
    
    arr[@]}
}

deal

Implementation modalities:

sh split.sh "1.1\t1669623112723\t\t\t10.93.192.245_10.93.192.249" '\\t'

Split result:

1.1 1669623112723 10.93.192.245_10.93.192.249

Guess you like

Origin blog.csdn.net/qq_34417408/article/details/128864384