无法在方括号在bash中提取数据 - unable to extract data inside square brackets in bash

- 此内容更新于:2015-12-20
主题:

  我已经输入(输出。txt等近2000行)
  loremipsum
  loremipsum
  (“a”、“b”、“c”,“d”)
  loremipsum
  loremipsum
  ["e",“f”,“g”,“h”)
  我的output1。txt应该
  一个
  b
  c
  d
  e
  f
  g
  h
  首先,我想把所有值在[]在一个文件中。但最终我的目标是实现output1.txt。这将是伟大的如果有人帮我实现一箭([]内提取数据和删除”“和逗号和地点值在每一行)现在我的代码的文件不是在指定路径中创建和终端没有错误。我跟着stackoverflow后写上面的代码shell脚本的链接。如何使用正则表达式提取字符串正则表达式提取文本方括号之间

原文:

I have input(output.txt with almost 2000lines) like

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

My output1.txt should be

a
b
c
d
e
f
g
h

First, i am trying to put all values inside [] in one file. But ultimately my aim is to achieve output1.txt. It would be great if someone help me to achieve in one shot(extracting data inside [] and removing "" and comma and place values in each line)

My code is as of now

    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

File is not created in specified path and also on the terminal there is no error. I followed following stackoverflow links to write above code

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

网友:你用来代替。你会让事情更容易对自己如果你测试代码链接到第一,确保它是有效的,然后逐渐适应它,清楚地看到此时你卡住

(原文:You used = ~$reg instead of =~ $reg. You'll make things easier on yourself if you test the code you link to first to make sure it works, then gradually adapt it and see exactly at which point you get stuck)

楼主:太棒了。谢谢。你是对的。你能告诉我我应该如何提取output1.txt吗

(原文:Awesome..Thanks.You are right. Can you tell me how i should extract like output1.txt)

楼主:我得到错误消息extract1。承宪:第3行:意想不到的EOF在寻找匹配的“extract1。承宪:13号线:语法错误:意想不到的文件结束我跟着这个链接[stackoverflow.com/questions/171480/?

(原文:reg="\[([^]]+)\]" reg1="(["'])(?:(?=(\\?))\2.)*?\1" while IFS='' read -r line || [[ -n "$line" ]]; do if [[ $line =~ $reg ]] then if [[ $line =~ $reg1 ]] then echo "1" echo "$line" >> /home/hdpsrvc/sandeep/hbase/output1.txt fi fi done < /home/hdpsrvc/sandeep/hbase/output.txt I am getting error message extract1.sh: line 3: unexpected EOF while looking for matching `'' extract1.sh: line 13: syntax error: unexpected end of file I followed this link[stackoverflow.com/questions/171480/…)

解决方案:
一个很容易这样做:
原文:

An awk can do this easily:

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
楼主:不,它的输入与输出。txt(行变量从output.txt读取每一行)。现在你能告诉我去哪里把output.txt吗?

(原文:No, its taking input from output.txt(line variable reads each and every line from output.txt). so now can you tell me where to put output.txt?)

楼主:我理解output1。三是我的输出。但我output.txt输入文件。如果你看到我的代码有两个不同的文件。我问output.txt。谢谢你的时间和精力

(原文:i understood output1.txt is my output. But my input file is output.txt. If you see my code there are 2 different files. I am asking about output.txt. Thanks for your time and effort)

楼主:对不起,让您费心了。我的代码在extract.sh问题是。所以我可以删除我的整个代码和awk代码?

(原文:sorry for troubling you. my code in the question is in extract.sh. So can i remove my entire code and place your awk code?)

网友:是的当然,删除所有的代码和使用awk。还要检查这个演示

(原文:Yes of course, remove all that code and use this awk. Also check this demo)

楼主:哇。超级。谢谢你的时间和精力。也是一个非常大的谢谢你修改你的答案根据我的要求。我接受你的答案。谢谢你再一次

(原文:wow. super. Thanks for your time and effort. Also a very big thanks for revising your answer according to my requirement. I am accepting your answer. Thanks once again)