2
我有一个文件,其内容类似如下。sed命令/ shell脚本读取特定行并在需要时进行更新
name: MyName
age: 25
subject: Math
这个文件需要被更新为:
name: MyName
age: "25"
subject: Math
但条件是,sed命令/ shell脚本可以运行多次。但是,双引号只能添加一次。
我为它写了一个脚本,它的工作原理。想找到一个更简单的解决方案。
#!/bin/bash
FILE="myfile"
while IFS='' read -r line || [[ -n "$line" ]]; do
if [[ $line =~ 'age:' ]]
then
if ! [[ $line =~ 'age: "' ]]
then
sed 's/\(age:[[:blank:]]*\)\(.*\)/\1"\2"/' -i $FILE
fi
fi
done < $FILE
是的,工作就像一个魅力。谢谢 – Chetan