РЕДАКТИРОВАТЬ: Так как OP говорит теперь, что номер поля строки может отличаться, поэтому я ищу строку BUMPCASE
здесь с цифрами в строке, которая начинается с #
.
awk '/^#/ && match($0,/BUMPCASE[0-9]+/){val=substr($0,RSTART,RLENGTH);next} NF{$NF=$NF":"val} NF' Input_file
Может помочь вам следующее awk
.
Решение 1-е: В случае, если вы хотите удалитьпустые строки.
awk '/^#/{val=$4;next} NF{$NF=$NF":"val} NF' Input_file
Объяснение:
awk '
/^#/{ ##Checking condition if a line starts from #(hash) then do following.
val=$4; ##Assigning a variable named val to 4th field value in current line.
next} ##next keyword will skip all further statements from here.
NF{ ##NF is a condition which checks if a line is NOT NULL then do following.
$NF=$NF":"val}##Appending colon and value of variable val here to last field of line.
NF ##NF condition checks if a line is NOT NULL then print the current line.
' Input_file ##Mentioning Input_file name here.
Решение 2-е: Если вы хотите сохранить пустые строки в выводе.
awk '/^#/{val=$4;next} NF{$NF=$NF":"val} 1' Input_file
Объяснение:
awk '
/^#/{ ##Checking condition if a line starts from #(hash) then do following.
val=$4; ##Assigning a variable named val to 4th field value in current line.
next} ##next keyword will skip all further statements from here.
NF{ ##NF is a condition which checks if a line is NOT NULL then do following.
$NF=$NF":"val}##Appending colon and value of variable val here to last field of line.
1 ##awk works on method of condition and action so putting 1 making condition TRUE so print of line will happen.
' Input_file ##Mentioning Input_file name here.