【发布时间】:2022-08-18 17:51:05
【问题描述】:
我在linux中有一个包含以下内容的文件:
servername: tesing1001
os: Unix
bu:
aid: 132
location: anywhere
environment: dev-stg
application: ABCD testing space
city: kk
我想用以下数据替换同一文件中的内容:
servername: tesing1001
os: Unix
**bu: BLANK** **>>>>here since value is empty i will set it as BLANK hardcoded**
aid: 132
location: anywhere
environment: dev-stg
**application: ABCD_testing_space** **>>>>>here we will replace string space with \"_\"**
city: kk
我们将在同一个文件中完成所有这些事情。所以,到目前为止,我正试图通过以下逻辑实现上述输出
#!/bin/bash
cp -p /opsunix/dyfacter.txt /tmp/customized.txt.tmp
awk -F \":\" \'{
if ($2 == \"\")
{
print $0 \" blank\"
} else {
print $0
}
}\' /opsunix/dyfacter.txt > /tmp/customized.txt.tmp && mv /tmp/customized.txt.tmp /opsunix/dyfacter.txt
借助上述代码,我能够识别空值并将其替换为\“空白的\”细绳。
servername: tesing1001
os: Unix
**bu: blank** **>>>>done**
aid: 132
location: anywhere
environment: dev-stg
**application: ABCD testing space** **>>>>>still need to correct**
city: kk
但是倒数第二行,即:应用:ABCD测试空间我无法将其转换为应用程序:ABCD_testing_space.
应用 sed -i \'s/ /_/g\' /opsunix/dyfacter.txt 正在替换文件中的所有空格 : 。
目的是替换字符串中的空格。
请帮忙!