Home > Blockchain >  Replace string bash script with sed
Replace string bash script with sed

Time:07-21

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!

CodePudding user response:

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 ]

CodePudding user response:

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

CodePudding user response:

Try this command:

sed -E '/^VERSION/{ s/([0-9.] )/"\1"/g; s/\[(. )\]/[ \1 ]/; }' input.txt
  • /^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

CodePudding user response:

Using sed

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

CodePudding user response:

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.

  • Related