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

发布于 2024-12-10 11:40:11 字数 328 浏览 0 评论 0原文

我有这个文件,我只想获取 testme= 的值,以便我可以执行另一个操作。但这会抛出很多行,实际上还无法使其工作。

1.test.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

I have this file and i want to only get the value of testme= So that i can do another action. But this throws lots of lines and actually cant yet make it work.

1. test.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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

总攻大人 2024-12-17 11:40:12

怎么样

#!/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

How about

#!/bin/bash

grep 'testme=' /var/tmp/test.ini | awk -F= '{ print  $2 }'

or alternatively just using bash

#!/bin/bash

regex='testme=(.*)'

for i in $(cat /var/tmp/test.ini);
do
    if [[ $i =~ $regex ]];
    then
        echo ${BASH_REMATCH[1]}
    fi
done
他夏了夏天 2024-12-17 11:40:12

我检查了你的代码,问题出在你的for循环中。

您实际上读取了文件的每一行,并将其交给 grep,这是不正确的。我猜你有很多行有错误,

没有这样的文件或目录

(或类似的东西)。

你应该给 grep 你的文件名。 (没有 for 循环)

例如

grep "testme=" /var/tmp/test.ini

I checked your codes, the problem is in your for loop.

you actually read each line of the file, and give it to grep, which is NOT correct. I guess you have many lines with error,

no such file or directory

(or something like that).

you should give grep your file name. (without the for loop)

e.g.

grep "testme=" /var/tmp/test.ini
万劫不复 2024-12-17 11:40:12
grep -v '^;' /tmp/test.ini | awk -F= '$1=="testme" {print $2}'

grep 删除注释,然后 awk 查找变量并打印其值。或者,在 awk 行中执行同样的操作:

awk -F= '/^\s*;/ {next} $1=="testme" {print $2}' /tmp/test.ini 
grep -v '^;' /tmp/test.ini | awk -F= '$1=="testme" {print $2}'

The grep removes comments, then awk finds the variable and prints its value. Or, same thing in a single awk line:

awk -F= '/^\s*;/ {next} $1=="testme" {print $2}' /tmp/test.ini 
不忘初心 2024-12-17 11:40:12

这个怎么样?

$ grep '^testme=' /tmp/test.ini  | sed -e 's/^testme=//' 
value1

我们找到该行,然后删除前缀,只留下值。 Grep 为我们进行迭代,无需明确。

How about this?

$ grep '^testme=' /tmp/test.ini  | sed -e 's/^testme=//' 
value1

We find the line and then remove the prefix, leaving only the value. Grep does the iterating for us, no need to be explicit.

梦忆晨望 2024-12-17 11:40:12

awk 可能是正确的工具,但由于问题似乎暗示您只想使用 shell,因此您可能需要类似的东西:

while IFS== read lhs rhs; do
  if test "$lhs" = testme; then
     # Here, $rhs is the right hand side of the assignment to testme
  fi
done < /var/tmp/test.ini

awk is probably the right tool for this, but since the question does seem to imply that you only want to use the shell, you probably want something like:

while IFS== read lhs rhs; do
  if test "$lhs" = testme; then
     # Here, $rhs is the right hand side of the assignment to testme
  fi
done < /var/tmp/test.ini
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文