linux - 如何使用 Bash 读取特定行和特定字段?

标签 linux bash sh bash4

我有这个文件,我只想获取 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/

相关文章:

linux - 使用 screen 启动 minecraft 服务器(linux debian)

macos - 用于打印当前正在运行的所有应用程序的 Bash 命令

python os.system 命令跳过与命令中的 awk 关联的空格 ""

linux - bash 中的 PHP preg_replace,具体情况

linux - cmake "make install"到远程机器?

linux - 如何检查 rmdir 是否返回 EEXIST 或 ENOTEMPTY?

linux - 具有 undefined variable 的 Ansible 模板

java - 如何应对交互式 Linux 命令?

linux - 如何替换变量中的脚本并在脚本中使用变量?

linux - 显示所有 1 行 php 文件的 Shell 脚本