РЕДАКТИРОВАТЬ: , поскольку OP изменил требование, поэтому добавление этого решения теперь тоже. проверил это с GNU awk
. Добавьте > temp_file && mv temp_file Input_file
на случай, если вы хотите сохранить вывод в самом файле Input_file.
awk -F'=' 'FNR==NR{if($0~/root/){value=$2};nextfile} /root/{$2=value} 1' OFS="=" parameter_file Input_file
Объяснение: Добавление пояснения к приведенному выше коду тоже здесь.
awk -F'=' ' ##Mentioning field separator as = here for all lines of all mentioned passed Input_files to awk.
FNR==NR{ ##Checking condition FNR==NR which will be TRUE when parameter_file is being read.
if($0~/root/){ ##Checking if a line has root string in it then do following.
value=$2 ##Assigning value of $2 to variable value here.
}
nextfile ##nextfile will jump to next passed Input_file and all further lines for parameter file will be skipped.
}
/root/{ ##Checking if a line has string root in it then do following.
$2=value ##Setting 2nd field value as value variable here.
}
1 ##By mentioning 1 telling awk to print edited/no-edited lines here.
' OFS="=" parameter_file Input_file ##Mentioning OFS value as = here and mentioning Input_file(s) name here.
Это должна быть простая sed
программа. Используйте параметр sed -i
на случай, если вы хотите сохранить вывод в самом файле Input_file.
sed '/root=/s/path1/path2/' Input_file
Если вы хотите использовать awk
, вам может помочь следующее.
awk '/root=/{sub("path1","path2")} 1' Input_file > temp_file && mv temp_file Input_file