less: поиск с игнорированием специальных символов

sed -e :a -e '$!N;s/^\(REF\*CE.*\)\n\(REF\*1W.*\)/\2\n\1/;ta' -e 'P;D' <testfile.txt 
  1. Если мы не в последней строке, то добавляем следующую строку.
  2. Выполните замену в текущей строке, которая происходит только в том случае, если она соответствует substring containing pattern 1 + newline + substring containing pattern 2. Подстановка переворачивает две подстроки -. После замены вернитесь к метке :a.
  3. Если совпадений нет. Печатать пространство шаблонов как есть. Затем удалите пространство шаблона и снова запустите цикл.

Образец с некоторыми окружающими линиями...

In:

    XEF*CE*------------------------- 
    REF*CE*------------------------- 
    REF*1W*------------------------- 
    REF*2W*------------------------- 

Out:


    XEF*CE*------------------------- 
    REF*1W*------------------------- 
    REF*CE*------------------------- 
    REF*2W*------------------------- 

В более общем случае для любого шаблона1 и шаблона2

sed -e :a \
    -e "\$!N; s/^\(.*${pattern1}.*\)\n\(.*${pattern2}.*\)/\2\n\1/;ta" \
    -e 'P;D' < inputfile
3
26.07.2019, 13:40
2 ответа

попробуйте -Rили -rпереключите -вы сможете правильно видеть цвета (или просто представить их ), так как он будет интерпретировать побеги "правильно".

Отlessмужчина:

   -r or --raw-control-chars
          Causes "raw" control characters to be displayed.  The default is to display control characters using the caret notation; for  example,
          a control-A (octal 001) is displayed as "^A".  Warning: when the -r option is used, less cannot keep track of the actual appearance of
          the screen (since this depends on how the screen responds to each type of control character).   Thus,  various  display  problems  may
          result, such as long lines being split in the wrong place.


   -R or --RAW-CONTROL-CHARS
          Like -r, but only ANSI "color" escape sequences are output in "raw" form.  Unlike -r, the screen appearance is maintained correctly in
          most cases.  ANSI "color" escape sequences are sequences of the form:

               ESC [... m

          where the "..." is zero or more color specification characters For the purpose of keeping  track  of  screen  appearance,  ANSI  color
          escape sequences are assumed to not move the cursor.  You can make less think that characters other than "m" can end ANSI color escape
          sequences by setting the environment variable LESSANSIENDCHARS to the list of characters which can end a color escape  sequence.   And
          you  can  make less think that characters other than the standard ones may appear between the ESC and the m by setting the environment
          variable LESSANSIMIDCHARS to the list of characters which can appear.
0
27.01.2020, 21:46

В качестве обходного пути вы можете поместить .*между словами, что пропустит все между ними, включая управляющие символы.

Таким образом, вместо поиска lorem ipsum dolor rosatвы будете искать lorem.*ipsum.*dolor.*rosat.

Отказ от ответственности :Обратите внимание, что он может соответствовать некоторым нежелательным последовательностям, но это не должно быть большой проблемой для нескольких искомых слов, поскольку совпадения находятся только в одной строке.

-1
27.01.2020, 21:46

Теги

Похожие вопросы