由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - A problem on string parsing (using either grep or perl)
相关主题
Usage of Grep???help!!!some problems with "cin"
大牛 in Perl and SED?How to concatenate two .tar.gz files
python gc question[合集] 如何能让这个程序快一点呢?太慢了
怎样才能用perl等东西知道c macro中的数值gvim diff question
问个关于正则表达式的超弱智问题...how to assign new value to loop variables?
What language I should use?如果python command line positional arguments 里有些是运算,那这种argument 该怎么处理?
perl: how to get the filename from the full path name问java api的问题
有人能解释一下这段C++代码吗Perl:如何处理这种hash 结构,
相关话题的讨论汇总
话题: data话题: opt话题: grep话题: perl话题: dim
进入Programming版参与讨论
1 (共1页)
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\./;

1 (共1页)
进入Programming版参与讨论
相关主题
Perl:如何处理这种hash 结构,问个关于正则表达式的超弱智问题...
how to count the times a function is usedWhat language I should use?
天,如何能让程序转得快点?有包子。perl: how to get the filename from the full path name
Help: How to extract the numberic value in a sentence?有人能解释一下这段C++代码吗
Usage of Grep???help!!!some problems with "cin"
大牛 in Perl and SED?How to concatenate two .tar.gz files
python gc question[合集] 如何能让这个程序快一点呢?太慢了
怎样才能用perl等东西知道c macro中的数值gvim diff question
相关话题的讨论汇总
话题: data话题: opt话题: grep话题: perl话题: dim