使用sed从特定的对分隔符之间获取文本(Getting text from between a specific pair delimiters using sed)

编程入门 行业动态 更新时间:2024-10-27 07:32:40
使用sed从特定的对分隔符之间获取文本(Getting text from between a specific pair delimiters using sed)

我是第一次使用sed所以它甚至可能不是适合这项工作的工具,但经过广泛的谷歌搜索后,似乎可以完成这项工作。 我的问题是我有一个数据文件,我需要从中提取一些数据并丢弃其余数据。

样本数据

#Ranger (62:0) Device pose: (0, 0, 0), (0, 0, 0) Device size: (0, 0, 0) Configuration: Minimum angle: 0 Maximum angle: 0 Angular resolution: 0 Minimum range: 0 Maximum range: 0Range resolution: 0 Scanning frequency: 0 682 range readings: [0.529, 0.524, 0.511, 0.506, 0.505, 0.505, 0.505, 0.505, 0.505, 0.503, 0.495, 0.483, 0.471, 0.469, 0.469, 0.469, 0.469, 0.465, 0.458, 0.458, 0.454, 0.454, 0.454, 0.45, 0.443, 0.442, 0.442, 0.443] #Ranger (62:0) Device pose: (0, 0, 0), (0, 0, 0) Device size: (0, 0, 0) Configuration: Minimum angle: 0 Maximum angle: 0 Angular resolution: 0 Minimum range: 0 Maximum range: 0Range resolution: 0 Scanning frequency: 0 682 range readings: [0.529, 0.524, 0.511, 0.506, 0.505, 0.505, 0.505, 0.505, 0.505, 0.503, 0.495, 0.483, 0.471, 0.469, 0.469, 0.469, 0.469, 0.465, 0.458, 0.458, 0.454, 0.454, 0.454, 0.45, 0.443, 0.442, 0.442, 0.443]

这种模式重复了很多次。 我想要的数据在[和]之间。 我想要所有[]对之间的所有数据。 我已经尝试了一些sed脚本,包括一个作为一个非常类似的问题的解决方案发送,但无济于事。 剧本

sed -n -e '/\[[^]]/s/^[^[]*\[\([^]]*\)].*$/\1/p' <a.txt >b.txt

产生一个空的b.txt。 然后我试了一下

sed -e '1s/#/<rem>\n&/g' -e 's/\]/\n<rem>/g' -e 's/\[/<\/rem>\n/g' -e '/^$/d' -e 's/[ ]*//g' <a.txt > b.txt

这产生了由<rem>和</rem>标签包围的漂亮的分隔数据块,如此

<rem> #Ranger(62:0) Devicepose:(0,0,0),(0,0,0) Devicesize:(0,0,0) Configuration: Minimumangle:0 Maximumangle:0 Angularresolution:0 Minimumrange:0 Maximumrange:0Rangeresolution:0 Scanningfrequency:0 682rangereadings: </rem> 0.529,0.524,0.511,0.506,0.505,0.505,0.505,0.505,0.505,0.503,0.495,0.483,0.471,0.469,0.469,0.469,0.469,0.465,0.458,0.458,0.454,0.454,0.454,0.45,0.443,0.442,0.442,0.443,0.451,0.459,0.459 <rem> #Ranger(62:0) Devicepose:(0,0,0),(0,0,0) Devicesize:(0,0,0)

在此之后我尝试

sed -e '/<rem>/,/<\/rem>/d' <b.txt >c.txt

我明白了

#Ranger(62:0) Devicepose:(0,0,0),(0,0,0) Devicesize:(0,0,0) Configuration: Minimumangle:0 Maximumangle:0 Angularresolution:0 Minimumrange:0 Maximumrange:0Rangeresolution:0 Scanningfrequency:0 682rangereadings: #Ranger(62:0) Devicepose:(0,0,0),(0,0,0) Devicesize:(0,0,0)

与我想要实现的完全相反。 有人可以帮忙吗? 很抱歉很长的解释。

I am using sed for the first time so it might not even be the right tool for the job but after extensive googling it seems to be what would do the job. My problem is that i have a data file from which I need to extract some data and discard the rest.

Sample data

#Ranger (62:0) Device pose: (0, 0, 0), (0, 0, 0) Device size: (0, 0, 0) Configuration: Minimum angle: 0 Maximum angle: 0 Angular resolution: 0 Minimum range: 0 Maximum range: 0Range resolution: 0 Scanning frequency: 0 682 range readings: [0.529, 0.524, 0.511, 0.506, 0.505, 0.505, 0.505, 0.505, 0.505, 0.503, 0.495, 0.483, 0.471, 0.469, 0.469, 0.469, 0.469, 0.465, 0.458, 0.458, 0.454, 0.454, 0.454, 0.45, 0.443, 0.442, 0.442, 0.443] #Ranger (62:0) Device pose: (0, 0, 0), (0, 0, 0) Device size: (0, 0, 0) Configuration: Minimum angle: 0 Maximum angle: 0 Angular resolution: 0 Minimum range: 0 Maximum range: 0Range resolution: 0 Scanning frequency: 0 682 range readings: [0.529, 0.524, 0.511, 0.506, 0.505, 0.505, 0.505, 0.505, 0.505, 0.503, 0.495, 0.483, 0.471, 0.469, 0.469, 0.469, 0.469, 0.465, 0.458, 0.458, 0.454, 0.454, 0.454, 0.45, 0.443, 0.442, 0.442, 0.443]

This pattern is repeated quite a few number of times. The data that I want is between the [ and ]. I want all the data between all the [ ] pairs. I have tried a few sed scripts including one that was sent as a solution to a very similar problem but to no avail. The script

sed -n -e '/\[[^]]/s/^[^[]*\[\([^]]*\)].*$/\1/p' <a.txt >b.txt

produces an empty b.txt. Then I tried

sed -e '1s/#/<rem>\n&/g' -e 's/\]/\n<rem>/g' -e 's/\[/<\/rem>\n/g' -e '/^$/d' -e 's/[ ]*//g' <a.txt > b.txt

Which produces nice delimited blocks of data that are surrounded by <rem> and </rem> tags like so

<rem> #Ranger(62:0) Devicepose:(0,0,0),(0,0,0) Devicesize:(0,0,0) Configuration: Minimumangle:0 Maximumangle:0 Angularresolution:0 Minimumrange:0 Maximumrange:0Rangeresolution:0 Scanningfrequency:0 682rangereadings: </rem> 0.529,0.524,0.511,0.506,0.505,0.505,0.505,0.505,0.505,0.503,0.495,0.483,0.471,0.469,0.469,0.469,0.469,0.465,0.458,0.458,0.454,0.454,0.454,0.45,0.443,0.442,0.442,0.443,0.451,0.459,0.459 <rem> #Ranger(62:0) Devicepose:(0,0,0),(0,0,0) Devicesize:(0,0,0)

After this when I try

sed -e '/<rem>/,/<\/rem>/d' <b.txt >c.txt

I get

#Ranger(62:0) Devicepose:(0,0,0),(0,0,0) Devicesize:(0,0,0) Configuration: Minimumangle:0 Maximumangle:0 Angularresolution:0 Minimumrange:0 Maximumrange:0Rangeresolution:0 Scanningfrequency:0 682rangereadings: #Ranger(62:0) Devicepose:(0,0,0),(0,0,0) Devicesize:(0,0,0)

Exactly the opposite of what I am trying to achieve. Can someone please help? Sorry for the long explanation.

最满意答案

也许这就是你想要的:

sed -nr 's/\s*\[([^\]+)\]/\1/p'

Maybe this is what you want:

sed -nr 's/\s*\[([^\]+)\]/\1/p'

更多推荐

本文发布于:2023-07-08 00:57:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1070180.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文本   分隔符   sed   text   delimiters

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!