РЕДАКТИРОВАТЬ 2: Или вы хотите заменить 123
каждой строки на 111
без проверки какого-либо условия, которое вы пробовали в awk
, затем просто выполните:
awk '{sub(/123/,"111")} 1' Input_file
Измените sub
на gsub
, если в одной строке много раз 123
.
Объяснение приведенного выше кода:
awk -v new_value="111" ' ##Creating an awk variable named new_value where OP could keep its new value which OP needs to be there in line.
/^Number/ { $NF=new_value } ##Checking if a line starts from Number string and then setting last field value to new_value variable here.
/^PID/ { num=split($NF,array,"/"); ##Checking if a line starts from PID then creating an array named array whose delimiter it / from last field value
array[2]=new_value; ##Setting second item of array to variable new_value here.
for(i=1;i<=num;i++){ val=val?val "/" array[i]:array[i] }; ##Starting a loop from 1 to till length of array and creating variable val to re-create last field of current line.
$NF=val; ##Setting last field value to variable val here.
val="" ##Nullifying variable val here.
}
1' Input_file ##Mentioning 1 to print the line and mentioning Input_file name here too.
РЕДАКТИРОВАТЬ: Если вам нужно набрать /
на выходе, используйте следующую команду awk
.
awk -v new_value="111" '
/^Number/ { $NF=new_value }
/^PID/ { num=split($NF,array,"/");
array[2]=new_value;
for(i=1;i<=num;i++){ val=val?val "/" array[i]:array[i] };
$NF=val;
val=""
}
1' Input_file
Следующие awk
могут вам в этом помочь. (Кажется, после того, как я применил теги кода к вашим образцам, ваш образец ввода немного изменился, так что теперь редактируйте мой код соответственно)
awk -F"[ /]" -v new_value="111" '/^Number/{$NF=new_value} /^PID/{$(NF-1)=new_value}1' Input_file
Если вы хотите сохранить изменения в самом файле Input_file, добавьте > temp_file &7 mv temp_file Input_file
в указанный выше код.
Объяснение:
awk -F"[ /]" -v new_value="111" ' ##Setting field separator as space and / to each line and creating awk variable new_value which OP wants to have new value.
/^Number/{ $NF=new_value } ##Checking condition if a line is starting with string Number then change its last field to new_value value.
/^PID/ { $(NF-1)=new_value } ##Checking condition if a line starts from string PID then setting second last field to variable new_value.
1 ##awk works on method of condition then action, so putting 1 making condition TRUE here and not mentioning any action so by default print of current line will happen.
' Input_file ##Mentioning Input_file name here.