如何使用 Linux/bash 命令打印两个字符之间的字符串

问题描述 投票:0回答:1

我正在尝试找到一个可以给我 [ 和 ] 之间的文本的 Linux 命令。我尝试了标准的 grep 和 sed 但没有任何帮助。假设文本在日志中。喜欢-

The list of tables you have currently [apple, pineapple, wood-apple]

我只想要这个文字[苹果,菠萝,木苹果]

目前已尝试过

  1. 回显“$LOGS”| grep -o "[.*]"
  2. 回显“$LOGS”| sed -n 's/.[([^]])].*/ /p'
  3. 回显“$LOGS”| grep -o '[\K[^]]+'
  4. 回显“$LOGS”| awk -F'[][]' '{for(i=2;i<=NF;i+=2) print $i}'

到目前为止,他们都没有提供帮助。谁能帮忙提供正确的命令来帮助我

linux bash cmd
1个回答
0
投票
$ echo "$logs" | grep -o '\[.*]'
[apple, pineapple, wood-apple]

$ echo "$logs" | sed -n 's/.*\(\[.*]\).*/\1/p'
[apple, pineapple, wood-apple]

$ echo "$logs" | awk -F'[][]' '{print "[" $2 "]"}'
[apple, pineapple, wood-apple]
© www.soinside.com 2019 - 2024. All rights reserved.