我是第一次使用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.txtproduces 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.txtWhich 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.txtI 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'更多推荐
发布评论