Several methods of shell replacement string, variable substitution $ {}, sed, awk

Variable is a string with spaces, now "hdpusr400" substitution variables in a "hduser302" with:

. 1 [liusiyi @ localhost ~] $ echo $ A 
2 -rw-R & lt ----- 3336 2019-07-12 17:35. 3 hduser302 hduser302 / Apps / hduser302 / Student / Properties / for xxyy / IMP_00004_XXYY_USE_PERSONALINFO.properties
 . 3  
. 4  # replaced with the variable $ {a //} do the replacement string 
. 5 {a / hduser302 / hdpusr400} [liusiyi @ localhost ~] $ echo $   # variable in a first matched string will be replaced 
. 6 -rw-R & lt hdpusr400 hduser302 3336 2019-07-12. 3 ----- 17:35 / Apps / hduser302 / Student / Properties / for xxyy / IMP_00004_XXYY_USE_PERSONALINFO.properties
 . 7 [liusiyi @ localhost ~] $ echo $ {// A hduser302 / hdpusr400}  # a variable all matching strings are replaced 
8----- hdpusr400. 3-R & lt -rw hdpusr400 3336 2019-07-12 17:35 / Apps / hdpusr400 / Student / Properties / for xxyy / IMP_00004_XXYY_USE_PERSONALINFO.properties
 . 9  
10  # use sed to make the replacement string 
. 11 [liusiyi ~ @ localhost ] $ echo $ a | Sed 'S / hduser302 / hdpusr400 /'   # Sed 'S ///' to replace the first matching string 
12 is -rw-R & lt hdpusr400 hduser302 3336 2019-07. 3 ----- 17:35 -12 / Apps / hduser302 / Student / Properties / for xxyy / IMP_00004_XXYY_USE_PERSONALINFO.properties
 13 is [liusiyi @ localhost ~] $ echo $ A | Sed 'S / hduser302 / hdpusr400 / G'   # Sed 'S /// G' It used to replace all matching strings 
14----- hdpusr400. 3-R & lt -rw hdpusr400 3336 2019-07-12 17:35 / Apps / hdpusr400 / Student / Properties / for xxyy / IMP_00004_XXYY_USE_PERSONALINFO.properties
 15  
16  # do replacement string (in this example by using sub awk or gsub may, by default a space as a separator) 
. 17 [liusiyi @ localhost ~] $ echo $ a | awk '{gsub (/ hduser302 /, " hdpusr400 " , $. 3 ); Print $ 0 }' # specify replace the first 
18 is -rw-R & lt ----- 3336 2019-07-12 17:35. 3 hdpusr400 hduser302 / Apps / hduser302 / Student / Properties / for xxyy / IMP_00004_XXYY_USE_PERSONALINFO.properties
 . 19 [liusiyi @ localhost ~] $ echo $ A | awk ' gsub {(/ hduser302 /, "hdpusr400 " ); Print $ 0 } '   # replace all 
20 is -rw-R & lt hdpusr400 hdpusr400. 3 ----- 3336 2019-07-12 17:35 /apps/hdpusr400/student/properties/xxyy/IMP_00004_XXYY_USE_PERSONALINFO.properties

 

END

 

Guess you like

Origin www.cnblogs.com/happyliusiyi/p/11227650.html