Должно быть проще с awk
.> tmp_file && mv tmp_file Input_file
предназначен для сохранения выходных данных в самом файле Input_file, он создаст временный файл и затем изменит его на фактический сам Input_file), попробуйте параметр gawk -i inplace
, несмотря на показанное ниже awk
, если оно у вас есть в вашей системе.
awk '/like/ && !/Linux/{$0=$0 " and Linux"} 1' Input_file > temp_file && mv temp_file Input_file
Объяснение: Ниже приведено объяснение и для приведенного выше кода.
awk '
/like/ && !/Linux/{ ##Checking condition if a line contains string like and DO NOT contain Linux then do following.
$0=$0 " and Linux" ##Re-creating $0(current line) with $0 value itself and string " and Linux" here as per OP request.
} ##Closing block for this condition now.
1 ##awk works on method of pattern and action, 1 means making condition/pattern TRUE and NO action mentioned so by default print will happen.
' Input_file ##Mentioning Input_file name here.