无法提取 bash 中方括号内的数据

unable to extract data inside square brackets in bash

我有输入(output.txt 将近 2000 行)喜欢

lorem ipsum 
lorem ipsum
["a","b","c","d"]
lorem ipsum
lorem ipsum
["e","f","g","h"]

我的output1.txt应该是

a
b
c
d
e
f
g
h

首先,我试图将 [] 内的所有值放在一个文件中。但最终我的目标是实现output1.txt。如果有人帮助我一次实现(在 [] 中提取数据并删除每行中的“”和逗号和位置值),那就太好了

我的代码是截至目前

    reg="\[([^]]+)\]"
    while IFS='' read -r line || [[ -n "$line" ]]; do
    if [[ $line = ~$reg ]] ; then echo "$line" >>      home/hdpsrvc/sandeep/hbase/output1.txt ; fi
    done < /home/hdpsrvc/sandeep/hbase/output.txt

文件没有在指​​定路径创建,而且在终端上也没有错误。我按照以下 Whosebug 链接编写了上面的代码

shell script. how to extract string using regular expressions Regular expression to extract text between square brackets

awk 可以轻松做到这一点:

cd /home/hdpsrvc/sandeep/hbase

awk -F'[][," ]+' '/\[.*\]/{for (i=2; i<NF; i++) print $i}' output.txt > output1.txt
a
b
c
d
e
f
g
h