[英]How get value from text file in linux
I have some file xxx.conf in text format. 我有一些文本格式的文件xxx.conf。 I have some text "disablelog = 1" in this file.
我在此文件中有一些文本“ disablelog = 1”。 When I use
当我使用
grep -r "disablelog" oscam.conf
output is 输出是
disablelog = 1
But i need only value 1. Do you have some idea please? 但是我只需要值1。请问您有什么主意吗?
one way is to use awk
to print just the value 一种方法是使用
awk
仅打印值
grep -r "disablelog" oscam.conf | awk '{print $3}'
you could also use sed
to replace diablelog =
with empty 您也可以使用
sed
将diablelog =
替换为空
grep -r 'disablelog' oscam.conf | sed -e 's/disablelog = //'
If you also want to get the lines with or without space before and after =
use 如果你也想有或没有空间之前和之后获得线
=
使用
grep -r 'disablelog' oscam.conf | sed 's/disablelog\s*=\s*//'
above command will also match 上面的命令也将匹配
disablelog=1
尝试:
grep -r "disablelog" oscam.conf | awk -F= '{print $2}'
Assuming you need it as a var in a script: 假设您需要在脚本中将其作为变量:
#!/bin/bash
DISABLELOG=$(awk -F= '/^.*disablelog/{gsub(/ /,"",$2);print $2}' /path/to/oscam.conf)
echo $DISABLELOG
When calling this script, the output should be 1. 调用此脚本时,输出应为1。
Edit: No matter wether there is whitespace or not between the equals sign and the value, the above will handle that. 编辑:无论等号与值之间是否存在空格,以上内容都可以解决。 The regex should be anchored in either way to improve performance.
正规表达式应以两种方式锚固以提高性能。
Just for fun a solution without awk
只是为了好玩而没有
awk
的解决方案
grep -r disablelog | cut -d= -f2 | xargs
xargs
is used here to trim the whitespace xargs
在这里用于修剪空白
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.