English 中文(简体)
删除两种模式(不包括模式)之间的所有线,使用ed或w
原标题:Delete all lines between two patterns (exclusive of the pattern) using sed or awk
  • 时间:2012-01-14 00:50:10
  •  标签:
  • regex
  • sed
  • awk

我的输出文本有相当多的内容,我需要删除两种模式之间的所有线,但保持模式匹配。

这些档案含糊地看着以下产出。

 TEST #1          
      coef1 |   48.36895    3.32013    14.57   0.000     41.86141    54.87649
      coef2 |  -50.08894   10.47335    -4.78   0.000    -70.61697   -29.56092
            |
  indicator |
         0  |   .6647992   2.646627     0.25   0.802     -4.55925    5.888849
         1  |   2.118701   5.225777     0.41   0.686     -8.19621    12.43361
            |
       year |
         2  |  -.4324005   2.231387    -0.19   0.847    -4.836829    3.972028
         3  |   -.362762    1.97184    -0.18   0.854    -4.254882    3.529358
            |
      _cons |   16.95753   6.342342     2.67   0.008     4.526383    29.38869
 TEST #2          
        coef2 |   48.36895    3.32013    14.57   0.000     41.86141    54.87649
        coef3 |  -50.08894   10.47335    -4.78   0.000    -70.61697   -29.56092
              |
         year |
           4  |   .6647992   2.646627     0.25   0.802     -4.55925    5.888849
           5  |   2.118701   5.225777     0.41   0.686     -8.19621    12.43361
              |
     idnumber |
           6  |  -.4324005   2.231387    -0.19   0.847    -4.836829    3.972028
           7  |   -.362762    1.97184    -0.18   0.854    -4.254882    3.529358
              |
        _cons |   16.95753   6.342342     2.67   0.008     4.526383    29.38869

我需要得出以下产出,删除“年”和“年”之间的所有线,但我需要保留从“条”开始的线。 预期成果如下:

 TEST #1          
      coef1 |   48.36895    3.32013    14.57   0.000     41.86141    54.87649
      coef2 |  -50.08894   10.47335    -4.78   0.000    -70.61697   -29.56092
            |
  indicator |
         0  |   .6647992   2.646627     0.25   0.802     -4.55925    5.888849
         1  |   2.118701   5.225777     0.41   0.686     -8.19621    12.43361
            |
       year |
      _cons |   16.95753   6.342342     2.67   0.008     4.526383    29.38869
 TEST #2          
        coef2 |   48.36895    3.32013    14.57   0.000     41.86141    54.87649
        coef3 |  -50.08894   10.47335    -4.78   0.000    -70.61697   -29.56092
              |
         year |
        _cons |   16.95753   6.342342     2.67   0.008     4.526383    29.38869

我写了以下文字(在文件十下):

sed  /^ +year/,/^ +_cons/{/^ +year/!{/^ +_cons/!d}}  input.txt >output.txt

但我有以下错误:

sed: 1: "/^ +year/,/^ +_cons/{/^ ...": extra characters at the end of d command

我不敢肯定这种做法是否正确,因为我似乎无法执行。 这里甚至合适吗?

最后一项说明是,我需要这一文字在相对通用的“团结”装置上工作。 我不得不向那些必须在非常基本的AIX(我认为)装置下执行这一规定的人发出这一信息。 在电子邮件上安装时,我不会 per,也不能 p。

最佳回答

在<代码>d后添加一个半殖民地,以表明指挥工作已经结束。 (GNUsed——唯一sed 我有手脚检验——是否要求这样做,但也许还有另一个<条码>:?

此外,如果你需要支持<代码><>ed的多种实施,那么你可以使用<代码>+,即“一个或多个”:它不是标准,也不是所有执行支持。 您可使用<代码>{1,},但这种编号只是粗略的。 页: 1

因此:

sed  /^ * year/,/^ * _cons/{/^ * year/!{/^ * _cons/!d;}}  input.txt >output.txt

(经测试,但仅使用GNUsed ,而不是OS X,当然不是AIX, sorry)。)

问题回答

这应当做到:

awk  /year/{print; getline; while($0!~/_cons/) {getline}}1  INPUT_FILE

<>>>

awk  /_cons/{print;f=0;next}/year/{f=1;print;next}f{next}1  INPUT_FILE

Following is the Output with your input-data file:

[jaypal:~/Temp] awk  /year/{print; getline; while($0!~/_cons/) {getline}}1  file
TEST #1          
      coef1 |   48.36895    3.32013    14.57   0.000     41.86141    54.87649
      coef2 |  -50.08894   10.47335    -4.78   0.000    -70.61697   -29.56092
            |
  indicator |
         0  |   .6647992   2.646627     0.25   0.802     -4.55925    5.888849
         1  |   2.118701   5.225777     0.41   0.686     -8.19621    12.43361
            |
       year |
      _cons |   16.95753   6.342342     2.67   0.008     4.526383    29.38869
 TEST #2          
        coef2 |   48.36895    3.32013    14.57   0.000     41.86141    54.87649
        coef3 |  -50.08894   10.47335    -4.78   0.000    -70.61697   -29.56092
              |
         year |
        _cons |   16.95753   6.342342     2.67   0.008     4.526383    29.38869

Test2:

[jaypal:~/Temp] awk  /_cons/{print;f=0;next}/year/{f=1;print;next}f{next}1  file
TEST #1          
      coef1 |   48.36895    3.32013    14.57   0.000     41.86141    54.87649
      coef2 |  -50.08894   10.47335    -4.78   0.000    -70.61697   -29.56092
            |
  indicator |
         0  |   .6647992   2.646627     0.25   0.802     -4.55925    5.888849
         1  |   2.118701   5.225777     0.41   0.686     -8.19621    12.43361
            |
       year |
      _cons |   16.95753   6.342342     2.67   0.008     4.526383    29.38869
TEST #2          
      coef2 |   48.36895    3.32013    14.57   0.000     41.86141    54.87649
      coef3 |  -50.08894   10.47335    -4.78   0.000    -70.61697   -29.56092
            |
       year |
      _cons |   16.95753   6.342342     2.67   0.008     4.526383    29.38869

这或许有助于你:

 sed  /year/,/_cons/{//!d}  file

 awk  /_cons/{p=0};!p;/year/{p=1}  file

You can do it visually. Just open the file with gVim, and run the command:

:g/^s*year/+1,/^s*_cons/-1 d

Explanation:

  • g global command
  • /^s*year/+1 match line bellow year
  • /^s*_cons/-1 match line above _cons
  • d delete the range

3. 总结和概括两个全球联盟的解决方案:

sed  /BEGIN/,/END/{/BEGIN/!{/END/!d;}}  input.txt
sed  /BEGIN/,/END/{//!d}  input.txt




相关问题
Uncommon regular expressions [closed]

Recently I discovered two amazing regular expression features: ?: and ?!. I was curious of other neat regex features. So maybe you would like to share some tricky regular expressions.

regex to trap img tag, both versions

I need to remove image tags from text, so both versions of the tag: <img src="" ... ></img> <img src="" ... />

C++, Boost regex, replace value function of matched value?

Specifically, I have an array of strings called val, and want to replace all instances of "%{n}%" in the input with val[n]. More generally, I want the replace value to be a function of the match ...

PowerShell -match operator and multiple groups

I have the following log entry that I am processing in PowerShell I m trying to extract all the activity names and durations using the -match operator but I am only getting one match group back. I m ...

Is it possible to negate a regular expression search?

I m building a lexical analysis engine in c#. For the most part it is done and works quite well. One of the features of my lexer is that it allows any user to input their own regular expressions. This ...

regex for four-digit numbers (or "default")

I need a regex for four-digit numbers separated by comma ("default" can also be a value). Examples: 6755 3452,8767,9865,8766,3454 7678,9876 1234,9867,6876,9865 default Note: "default" ...

热门标签