简体   繁体   English

在bash的第一个管道之间提取东西?

[英]Extract stuff between the first pipes in bash?

I want to extract the stuff in between the first two pipes: 我想提取前两个管道之间的内容:

| a650f360-a29d-4562-b46f-024fe15aa7dc | emi-ubuntu-12.04.2-server-amd64-08152013           | ACTIVE |

The output should be: 输出应为:

 a650f360-a29d-4562-b46f-024fe15aa7dc 

I have a regex that I was going to use sed with: ^\\|\\s*(.*?)\\s*\\| 我有一个正则表达式,我打算将其用于sed: ^\\|\\s*(.*?)\\s*\\|

but according to a regex calculator it gets me the pipes as well. 但是根据一个正则表达式计算器,它也可以帮助我。 How do I get rid of it? 我如何摆脱它?

Cut is probably easier to understand than regex. 剪切可能比正则表达式更容易理解。 Why not 为什么不

cut -d\| -f2

?

sed 's/^[^|]*|\([^|]*\)|.*/\1/'

它与开头的所有非管道匹配(示例数据中未包含任何内容),一个管道,捕获不是管道的东西,匹配另一个管道以及末尾的任何东西,并将其替换为捕获的东西。

Bash only. 仅重击。 In a loop this can be done using an array: 在循环中,可以使用数组完成此操作:

while IFS='|'; read -a line ; do
  echo ${line[1]}            # with leading/trailing blanks
  echo ${line[1]// /}        # without leading/trailing blanks
done < "$infile"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM