Добавить некоторый текст после 4 строк шаблона соответствия с помощью оболочки

Если вы хотите анализировать несколько журналов одновременно, вы можете использовать следующий синтаксис:

tail -f /var/log/{log1,log2}

Это приведет к желаемому выводу всех необходимых журналов, поэтому вы можете использовать аргумент -eв grepдля создания нескольких шаблонов. Так что все вместе:

tail -f /var/log/{log1,log2} | grep -ie "pattern1" -ie "pattern2" -ie "pattern3"

Что касается окрашивания grep, взгляните на этот отличный ответ.

0
26.08.2019, 13:41
1 ответ

Просто попытался настроить ваш существующий код, а не пробовать новый... Попробуйте это,

#!/bin/bash
count=0
IR=`grep -n "<name>Total Invoices Released</name>" BalanceForm.xml | cut -d: -f 1`
VIR=`grep -n "<name>Total Deep Invoice Released</name>" BalanceForm.xml | cut -d: -f 1`
while IFS= read -r line; do
NAME="`echo "$line" | awk '{$1=""; print}'`"
TOTAL=`expr $IR + 4 + $count`
TOT=`expr $VIR + 4 + $count + $count`
sed -i "{
       ${TOTAL}i\<operand>-Total Licensed Original $NAME</operand>
       ${TOTAL}i\<operand>-Total Licensed Reversal $NAME</operand>
       ${TOT}i\<operand>-Total Deep Licensed Original $NAME</operand>
       ${TOT}i\<operand>-Total Deep Licensed Reversal $NAME</operand>
     }" BalanceForm.xml
count=$(expr $count + 2)
done < file.txt
  • продолжайте тикать счетчик и добавляйте к номеру строки, так как номер строки будет увеличиваться при каждом добавлении

Выход:

<Formula>
<name>Total Invoices Released</name>
<comment></comment>
<sumType>Suspense</sumType>
<operands>
<operand>-Total Licensed Original  This is He</operand>
<operand>-Total Licensed Reversal  This is He</operand>
<operand>-Total Licensed Original  This is She</operand>
<operand>-Total Licensed Reversal  This is She</operand>
<operand>Total Hui Invoice Released</operand>
<operand>Total hdrt Invoices Released</operand>
<operand>Total hgfyu Invoices Released</operand>




<name>Total Deep Invoice Released</name>
<comment></comment>
<sumType>Suspense</sumType>
<operands>
<operand>-Total Deep Licensed Original  This is He</operand>
<operand>-Total Deep Licensed Reversal  This is He</operand>
<operand>-Total Deep Licensed Original  This is She</operand>
<operand>-Total Deep Licensed Reversal  This is She</operand>
<operand>-Total Deep Licensed Original </operand>
<operand>-Total Deep Licensed Original </operand>
<operand>-Total Deep Licensed Original </operand>
0
28.04.2021, 23:30

Теги

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