如何从Linux中的文本文件获取价值 [英] How get value from text file in linux

查看:43
本文介绍了如何从Linux中的文本文件获取价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些文本格式的文件xxx.conf.我在此文件中有一些文本"disablelog = 1".当我使用

I have some file xxx.conf in text format. I have some text "disablelog = 1" in this file. When I use

grep -r "disablelog" oscam.conf

输出为

disablelog = 1

但是我只需要值1.请问您有什么主意吗?

But i need only value 1. Do you have some idea please?

推荐答案

假设您需要在脚本中将它作为变量:

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

调用此脚本时,输出应为1.

When calling this script, the output should be 1.

等号与值之间是否存在空格,上述内容都可以解决.regex应该以两种方式锚定以提高性能.

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.

这篇关于如何从Linux中的文本文件获取价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆