Как соединить строки, пересекающие линии в bash?

Лучший способ просмотреть каждое совпадение — добавить «c» в конец кода, а не переходить от строки -к строке -

.
%s/print\(.*\)/logging.info\(\1\)/gc
0
29.10.2019, 03:10
4 ответа

Вы можете использовать printfтак, как это обычно используется. Первая строка определяет формат, а следующие строки являются аргументами.

Пример:

#!/bin/bash
   printf '%s\n' "I am a too long sentence in line1, I need to switch to a new line."\
      "I'd like to be at the head of this line2, but actually there are 3 redundant spaces."\
            "I don't care how much indentation is used"\
 "on the next line."

Выход:

I am a too long sentence in line1, I need to switch to a new line.
I'd like to be at the head of this line2, but actually there are 3 redundant spaces.
I don't care how much indentation is used
on the next line.
1
28.01.2020, 02:39

Метод @Freddy основан на принципе prinf. И я нашел общий способ просто построить строку из нескольких строк и без отступов:

#!/bin/bash
concat() {
  local tmp_arg
  for each_arg in $@
  do
    tmp_arg=${tmp_arg}${each_arg}
  done
  echo ${tmp_arg}
}

# indent free
   str=$(concat "hello1\n" \
   "hello2\n" \
   hello3)
   printf $str"\n"

Результат:

hello1
hello2
hello3
0
28.01.2020, 02:39

@Anon Решение вашей проблемы на самом деле очень простое. Вы можете либо использовать метод функции, либо метод с одной плетью также работает для многострочного комментария -без использования printf в каждой строке.

Решение:

echo "This is the first line. " \
> "This is second line"

Выход:

This is first line. This is second line

Этот метод можно использовать для соединения нескольких строк в одной эхо-команде. Используя косую черту, вы получите вторую пустую строку, где вы можете добавить новые предложения.

0
28.01.2020, 02:39

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

#! /bin/bash

echo "This text has
several lines.
This is another
sentence."

         echo "This line is printed starting from the left margin
    and these lines
    are indented
    by four spaces"

printf "This text contains
not %d, but %d two substitution
fields.\n" 1 2

, что дает

This text has
several lines.
This is another
sentence.
This line is printed starting from the left margin
    and these lines
    are indented
    by four spaces
This text contains
not 1, but 2 two substitution
fields.
0
28.01.2020, 02:39

Теги

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