我有这个文件,我只想获取 testme= 的值,以便我可以执行其他操作。但这会抛出很多行,实际上还无法使其工作。
1。测试.sh
#!/bin/bash
for i in $(cat /var/tmp/test.ini); do
# just one output i need: value1
grep testme= $i
done
2。/var/tmp/test.ini
; comments
testme=value1
; comments
testtwo=value2
最佳答案
怎么样
#!/bin/bash
grep 'testme=' /var/tmp/test.ini | awk -F= '{ print $2 }'
或者只使用 bash
#!/bin/bash
regex='testme=(.*)'
for i in $(cat /var/tmp/test.ini);
do
if [[ $i =~ $regex ]];
then
echo ${BASH_REMATCH[1]}
fi
done
关于linux - 如何使用 Bash 读取特定行和特定字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7793978/