×

Loading...

Topic

This topic has been archived. It cannot be replied.
  • 工作学习 / IT技术讨论 / 遇到了一个vi的问题
    在一段文本中想同时match两个string, 举例

    first second third
    secong third forth

    我只想找同时有first和second的那一行(第一行),如何找。
    不能离开vi 用grep之类的命令
    • 没有试过,用 :/ "first second" 行不行?我是指将要查询的内容用单引号或双引号括起来? 请将试验结果告知,谢谢 :-)
      • "first second"实际上是一个字符串。我以前也碰过这个问题,不过好象没找到办法。guest其实想做什么呢?可以说来听听吗?干嘛非在vi里不可?
        • 举个例子
          如果在log file里面我收到很多不同类型的message, 每个message有他自己的标志,例如MSG_1,MSG_2,...,在每个message里面,都可能有一个类似于人名的特征字,我想在特定的message类型中查找这个人名,例如,MSG_1&PETER, 同时,我可能又要查找MSG_2&JOHN, 如果把每个不同的message生成不同的子文件,太麻烦了。
          • 我也估计到你应该是在做数据处理。不过我想了一下,似乎在vi里面要搜索满足两个条件的行是没有可能的。
            因为vi实际上是在调用ex行编辑器,而要找出同时满足两个条件的行的话,一定有一些行是要进入两次的。而ex的行扫描方式决定了不可能在一个搜索命令里面把某些行扫描两次,必须要用两个搜索命令并借助临时文件(不一定在磁盘上)。但是在vi里你没有办法这样做。
          • 为什么不用grep -e MSG_1 -e PETER logfile
            • 我不是说了吗。
              可能同时在一个文件里面,我查完了MSG_1&PETER之后,马上要查MSG_2&JOHN, ...可能还有别的,这样就要生程序都临时文件。
              • 那你在vi里不也是要敲完一次又一次?有什么区别?
                • 不一样
                  如果你生成不同的文件,每次查找不同的pattern,你必须调入不同的文件,不能同时调入所有的子文件
                  • 你就不能grep -e MSG_1 -e PETER logfile >> tmpfile; grep -e MSG_2 -e SIMOM logfile >> tmpfile; ...............?写个script吧,这种数据处理用vi是最麻烦的方法了。
                    • Great! 等于重新排了序! :-)
                    • 可是我事先并不知道MSG_1,MSG_2,PTER,JOHN之间谁和谁组合呀.
                      • 那你什么时候才知道?你总要在敲命令前知道吧?我的意思是用vi真的不合适做数据处理,不如写个小script。
                        • 比如
                          我在查MSG_1&PETER时,发现我需要查MSG_1&JOHN, 查MSG_1&JOHN时,发现我需要查MSG_2&JOHN...
                          • 那你就每次把grep的结果>>进一个文件好了,除非你想修改那个源文件。
                            • 是不是要离开vi grep?
                              我不想离开,而且我是根本前一个search的结果才能决定下一次search什么。不能事先确定
                              • 那你可以敲个:!暂时进入shell里面去grep,不过要小心尽快回来,不然容易搞混了出事,曾经有人试过进了shell又vi又进shell...如此多次,最后不小心把数据搞丢了。
                                • 啊,还是不对,写个小script吧:grep -e \<$1\> -e \<$2\> logfile >> tmpfile;grep -e \<$1\> -e \<$2\> tmpfile 这样就可以立即看见结果并写入tmpfile了。
                                  • 你还是没理解
                                    如果我些了一个tmp文件,发现我要找另外的,又要写一个tmp文件,...起步是很麻烦?
                                    • 你就不可以每次都添加到同一个tmpfile里去吗?
                                      • 添加到同一个tmp文件里,选的时候不又打架了吗?
                    • Stupid ah! The correct command should be : grep MSG_1 logfile|grep PETER
    • /first*+second/
      • 什么东西?
      • sorry, type, should be /first.*second/
        • don't forget the dot "." before "*" I suggest you read the man page for regular expressions man -s5 regex
          • .(dot)是什么意思?
          • dot is a special character in RE
            it means "matching for any characters", and "*" is a
            special character in RE too, it means "any repetition
            of the previous RE(could be 0)"
        • 好使,讲讲什么原理好吗?
          • vi supports basic regular expression,
            I don't like basic regular expression because it's too limited
            you should try out awk or nawk, it's a coooool gadget.
            awk and nawk both support extended RE, you can write
            very sophisticated text processing programs with them.
          • .(dot) means any single character or nothing. *(star) means repeat the precedence character.
      • 应该是 /first.*second 。这是个变通的办法,实际上还是在搜索一个字符串,不过应该是 /first.*second 和 /second.*first 都做一次,如果没有指定first一定在second前面的话。
        • Thanks! Learned a lot! :-)
    • thanks a lot, lumlum and InTheSky.
      • nothing. //hand lumlum //hand guest //hand 飞雪浮冰
        • You are welcome. I wish I could understand what I am talking about, xixi,,, lumlum ^_^
          • ?? you puzzled me...
    • /first second
      • 这样只会match那个first。
        • I tried it on Unix several minutes ago.. It works. and It doesn't find the second line "second first" .
          • how to match 'first third second'?
        • now I am 100% sure
        • sorry, I misunderstood question. lumlum is right
    • 研究了一下,最正确的表达应该是
      /s\<first\>.\<second\>
      如果用其他的,会把类似1first,2second之类的也算做match 成功
      • 对对。不过是不是应该是 s/\<first\>.*\<second\> 呢?
        • *有什么用呢?
          • * means repeat the former character. Because there may be more than one characters between the two object words.