0

I want to use sed in order to replace one path with a different path,

I have this log4j file:

# suppress inspection "UnusedProperty" for whole file
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
spark.log.path=/tmp/logs/spark
msg.layout=%d{yyyy-MM-dd HH:mm:ss,SSS} %-5p [%t] (%F:%L) : %m%n

Now I want to change the path after "spark.log.path=" to a new one, the log4j file is always changing so I don't want to replace the path string, I want to replace the path after matching the 'spark.log.path='

I tried this shell script but it doesn't work (exception):

origin_path='spark.log.path='
k8s_path='spark.log.path=/tmp/logs/spark/master'
sed -i 's/^'${origin_path}' .*$/'${k8s_path}'/' log4j.properties

Can Anyone see what am I missing?

Shahar Hamuzim Rajuan
  • 5,610
  • 9
  • 53
  • 91
  • apart from using [different delimiter](https://stackoverflow.com/documentation/sed/1096/substitution/12280/using-different-delimiters#t=201706261148032439234) to avoid conflict with `/` in search and replacement strings, you need to also [escape metacharacters](https://stackoverflow.com/questions/29613304/is-it-possible-to-escape-regex-metacharacters-reliably-with-sed) like `.` – Sundeep Jun 26 '17 at 11:50

2 Answers2

1

Just change the separators:

sed -i "s_^${origin_path}.*_${k8s_path}_" log4j.properties

You have a conflict vs the Log Path.

Juan Diego Godoy Robles
  • 14,447
  • 2
  • 38
  • 52
0

try:

awk -v new_path="spark.log.path=/tmp/logs/spark/master" '{sub(/spark.log.path.*/,new_path);print}'  Input_file > temp_file && mv temp_file Input_file

simply substituting the regex spark.log.path.* to new value of spark which is present of awk's variable named new_path then doing print. Taking all the lines output into temp_file and once this command is completed successfully then renaming the temp_file to Input_file.

PS: there are awk's versions which will be doing in-place update to the Input_file also, this above code reads first Input_file do parsing with changes and takes expected output into a temp file and then renames it to same Input_file.

RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93