如何使用shell脚本替换属性文件中的点

时间:2022-01-15 15:30:02

First of all I have seen questions similar to this one.I have followed the solutions from those threads.But still im not able to fix it.I want to remove dots(.) from the key of the property file .Can some one help to identify the issue

首先,我看到了类似的问题。我遵循了这些思路中的解决方案。但我还是没能修好它。我想从属性文件的键中删除dots(.) .是否有人能帮助识别问题

I have a property file tempds.properties

我有一个属性文件tempds.properties

#Fri Jan 30 07:37:25 CET 2015
ds.home=/opt/abc
mykey=/opt/xyz

I use another script test.sh

我使用另一个脚本test.sh。

cd /opt
chmod 777 tempds.properties

#awk -F= -vOFS="=" 'gsub(/\./,"_",$1)+1' tempds.properties
cat tempds.properties| sed 's/\./_/g' > .tempds.properties


echo "Processing  "
for i in {1..5}; do
    sleep 1
    echo "..........."


done

. /opt/tempds.properties
echo $ds_home
echo $mykey
echo "Process finishd"

Output :

输出:

root@onebox:/opt# sudo ./test.sh
Processing
...........
./test.sh: 2: /opt/tempds.properties: ds.home=/opt/abc: not found

/opt/xyz
Process finishd

2 个解决方案

#1


2  

You create .tempds.properties from tempds.properties but then you still invoke tempds.properties.

您创建.tempds。从tempds属性。属性,但仍然调用tempds.properties。

You may skip creating the temporary file altogether:

您可以跳过创建临时文件:

eval `sed 's/\./_/g' tempds.properties`

#2


0  

You can replace dots by tr command,

你可以用tr命令替换圆点,

tr '.' '_' .tempds.properties

#1


2  

You create .tempds.properties from tempds.properties but then you still invoke tempds.properties.

您创建.tempds。从tempds属性。属性,但仍然调用tempds.properties。

You may skip creating the temporary file altogether:

您可以跳过创建临时文件:

eval `sed 's/\./_/g' tempds.properties`

#2


0  

You can replace dots by tr command,

你可以用tr命令替换圆点,

tr '.' '_' .tempds.properties