himneh
himneh

Reputation: 116

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!

Upvotes: 2

Views: 516

Answers (4)

potong
potong

Reputation: 58351

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.

Upvotes: 1

arzyu
arzyu

Reputation: 25

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

Upvotes: 0

anubhava
anubhava

Reputation: 784898

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 ]

Upvotes: 1

RavinderSingh13
RavinderSingh13

Reputation: 133428

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

Upvotes: 0

Related Questions