s*****c 发帖数: 753 | 1 Hi, I have a few lines in a file that I would like to extract. It is very
simple, each line start with opt., like the following
opt.xxxxxxx.name
opt.xxxxxxx.tag
opt.xxxxxxx.data
....
where xxxxxxx can be any string.
It is easy to extract them with grep. however, the xxxxxx can sometimes
contains "." as well. I want to replace those "." in xxxxxxx with other
string (like "_"). Note that there are only a few known choices for the
field behind xxxxxx.
Anyone have a quick and easy solution? |
S**I 发帖数: 15689 | 2 Perl:
if(/^opt\..+\.\w+$/){
s/(?
}
【在 s*****c 的大作中提到】 : Hi, I have a few lines in a file that I would like to extract. It is very : simple, each line start with opt., like the following : opt.xxxxxxx.name : opt.xxxxxxx.tag : opt.xxxxxxx.data : .... : where xxxxxxx can be any string. : It is easy to extract them with grep. however, the xxxxxx can sometimes : contains "." as well. I want to replace those "." in xxxxxxx with other : string (like "_"). Note that there are only a few known choices for the
|
l********a 发帖数: 1154 | 3 2个find找到第一个.和最后一个.
然后截取中间的sub string做replace |
s*****c 发帖数: 753 | 4 Thanks for the reply, however, there could be another dot in the subsequent
field.
I only have the following entry:
opt.test.xxx.data
opt.test.xxx.dim.x
opt.test.xxx.dim.y
opt.test.xxx.dim.z
opt.test.xxx.length
opt.test.xxx.tree
【在 l********a 的大作中提到】 : 2个find找到第一个.和最后一个. : 然后截取中间的sub string做replace
|
s*****c 发帖数: 753 | 5 My data look like this
opt.test.xxx.data=some data
opt.test.xxx.dim.x=some data
opt.test.xxx.dim.y=some data
opt.test.xxx.dim.z=some data
opt.test.xxx.length=some data
opt.test.xxx.tree=some data
I already use grep to extract those lines from a file to a new file using
grep '\.roi\.' file1 > file2
I suppose I will use s/(?
something like [data|dim\.x|...] to look ahead?
【在 S**I 的大作中提到】 : Perl: : if(/^opt\..+\.\w+$/){ : s/(?: }
|
S**I 发帖数: 15689 | 6 lookahead is also necessary, so it should be
if(/^opt\..+\.(data|dim\.[xyz]|length|tree)/){
s/(?
}
if all these lines are already extracted, it's simply
s/(?
【在 s*****c 的大作中提到】 : My data look like this : opt.test.xxx.data=some data : opt.test.xxx.dim.x=some data : opt.test.xxx.dim.y=some data : opt.test.xxx.dim.z=some data : opt.test.xxx.length=some data : opt.test.xxx.tree=some data : I already use grep to extract those lines from a file to a new file using : grep '\.roi\.' file1 > file2 : I suppose I will use s/(?
|
a***y 发帖数: 2803 | 7 opt.test.xxx.data=some data
opt.test.xxx.dim.x=some data
opt.test.xxx.dim.y=some data
opt.test.xxx.dim.z=some data
opt.test.xxx.length=some data
opt.test.xxx.tree=some data
用了
grep '\.roi\.' file1 > file2 之后,在file2里面不就只剩下
test.xxx
test.xxx.dim
了吗?
【在 s*****c 的大作中提到】 : My data look like this : opt.test.xxx.data=some data : opt.test.xxx.dim.x=some data : opt.test.xxx.dim.y=some data : opt.test.xxx.dim.z=some data : opt.test.xxx.length=some data : opt.test.xxx.tree=some data : I already use grep to extract those lines from a file to a new file using : grep '\.roi\.' file1 > file2 : I suppose I will use s/(?
|
S**I 发帖数: 15689 | 8 try it by yourself and you'll see why you're wrong. :)
using
use
【在 a***y 的大作中提到】 : opt.test.xxx.data=some data : opt.test.xxx.dim.x=some data : opt.test.xxx.dim.y=some data : opt.test.xxx.dim.z=some data : opt.test.xxx.length=some data : opt.test.xxx.tree=some data : 用了 : grep '\.roi\.' file1 > file2 之后,在file2里面不就只剩下 : test.xxx : test.xxx.dim
|
s*****c 发帖数: 753 | 9 This does not work because I have extra field after opt.
As I said, suppose my field are
opt.test.100.0cm.dim.x
if I use your command, I got
opt.test_100_0cm.dim.x
if I change it to
s/(?
I would get
opt.test.100_0cm.dim_x
Any help?
【在 S**I 的大作中提到】 : lookahead is also necessary, so it should be : if(/^opt\..+\.(data|dim\.[xyz]|length|tree)/){ : s/(?: } : if all these lines are already extracted, it's simply : s/(?
|
S**I 发帖数: 15689 | 10 I don't know how you make your change work, but lookbehind assertion in Perl
has to be fixed length. It seems one replacement is not enough:
s/\.(?!(data|dim\.[xyz]|length|tree))/_/g;
s/opt_test_/opt\.test\./;
s/dim_/dim\./;
【在 s*****c 的大作中提到】 : This does not work because I have extra field after opt. : As I said, suppose my field are : opt.test.100.0cm.dim.x : if I use your command, I got : opt.test_100_0cm.dim.x : if I change it to : s/(?: I would get : opt.test.100_0cm.dim_x : Any help?
|
s*****c 发帖数: 753 | 11 That is a good solution.
BTW, is there any chance to run it in command line use pipeline?
Perl
【在 S**I 的大作中提到】 : I don't know how you make your change work, but lookbehind assertion in Perl : has to be fixed length. It seems one replacement is not enough: : s/\.(?!(data|dim\.[xyz]|length|tree))/_/g; : s/opt_test_/opt\.test\./; : s/dim_/dim\./;
|