简体   繁体   中英

Replace string bash script with sed

I am (unsuccessfully) trying to change my log format file. I have input text which include:

VERSION = [1.1, 2.2, 3.3, 4.4]

Cuz my client use split style with " delimiter so I want to use sed command to convert input into:

VERSION = [ "1.1", "2.2", "3.3", "4.4" ]

I have tried this but it not works:

sed 's/^\(VERSION = \[\).*\(\]$\)/\1\", \"\2/'

Can anyone help me, thanks in advance!

Better you match number and enclose them with double quotes in replacement:

s='VERSION = [1.1, 2.2, 3.3, 4.4]'
sed -E '/^ *VERSION =/ { s/[0-9]+(\.[0-9]+)?/"&"/g; s/\[/& /; s/\]/ &/; }' <<< "$s"

VERSION = [ "1.1", "2.2", "3.3", "4.4" ]

Here:

  • /^[[:blank:]]*VERSION =/ : Matches a line starting with optional spaces followed by VERSION = text
  • [0-9]+(\.[0-9]+)? : Matches an integer number or floating point number
  • "&" : Wraps matched string with "
  • s/\[/& / : Add a space after [
  • s/\]/ &/ : Add a space before ]

With your shown samples please try following awk code. Written and tested in GNU awk . Its using match function of GNU awk , where I am using regex (^[^[]*\[)([^]]*)(\])$ which creates 3 capturing groups and creates 3 values into array named arr1 and then we are using 2nd value of it as per requirement to add " to before and after it, finally in this program printing all 3 values.

Here is the Online demo for mentioned regex, where it shows 3 of the capturing groups created by regex.

awk -v s1="\"" '
{
  val=""
  match($0,/(^[^[]*\[)([^]]*)(\])$/,arr1)
  num=split(arr1[2],arr2,", ")
  for(i=1;i<=num;i++){
    val=(val?val ", ":"") (s1 arr2[i] s1)
  }
  print arr1[1] val arr1[3]
}
'  Input_file

OR to check if value starts from VERSION itself then try following:

awk -v s1="\"" '
{
  val=""
  match($0,/(^VERSION[^[]*\[)([^]]*)(\])$/,arr1)
  num=split(arr1[2],arr2,", ")
  for(i=1;i<=num;i++){
    val=(val?val ", ":"") (s1 arr2[i] s1)
  }
  print arr1[1] val arr1[3]
}
'  Input_file

Try this command:

sed -E '/^VERSION/{ s/([0-9.]+)/"\1"/g; s/\[(.+)\]/[ \1 ]/; }' input.txt
  • /^VERSION/{ X; X; } /^VERSION/{ X; X; } , run multiple sed commands (X) on lines starting with VERSION
  • s/([0-9.]+)/"\1"/g , add quotes to all version numbers
  • s/\[(.+)\]/[ \1 ]/ , add spaces next to brackets

Using sed

$ sed -E '/VERSION =/{s/(\[)(.*)(])/\1 \2 \3/;s/[0-9.]+/"&"/g}' input_file
VERSION = [ "1.1", "2.2", "3.3", "4.4" ]

This might work for you (GNU sed):

sed '/^VERSION =/s/[0-9.]\+/"&"/g' file

Match on a line containing VERSION = and surround all groups of digits and a period with double quotes.

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