Использование Awk / Sed для подстановки значения совпавшего шаблона в файле - PullRequest
0 голосов
/ 01 июня 2018

У меня есть приведенный ниже ввод в файле конфигурации

#bumpcase case ( BUMPCASE45678 ) some other text
fred fredm1989

chasi chasi1987

hector hector1978
#bumpcase case ( BUMPCASE3123098 ) some other text
simon sim1984

roger roger1985

мне нужно отобразить содержимое в новом файле, как показано ниже

fred fredm1989:BUMPCASE45678

chasi chasi1987:BUMPCASE45678

hector hector1978:BUMPCASE45678

simon sim1984:BUMPCASE3123098

roger roger1985:BUMPCASE3123098

есть ли способ использовать awk / sedвыполнить то же самое?спасибо

Ответы [ 2 ]

0 голосов
/ 01 июня 2018

РЕДАКТИРОВАТЬ: Так как 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.
0 голосов
/ 01 июня 2018

Вы можете сделать следующее в awk:

awk '/^#/{bumpcase=$4}/^[^#]/{print $0":"bumpcase}' yourfile

Пример:

$ cat test.txt
#bumpcase case ( BUMPCASE45678 ) some other text
fred fredm1989

chasi chasi1987

hector hector1978
#bumpcase case ( BUMPCASE3123098 ) some other text
simon sim1984

roger roger1985

$ awk '/^#/{bumpcase=$4}/^[^#]/{print $0":"bumpcase}' test.txt
fred fredm1989:BUMPCASE45678
chasi chasi1987:BUMPCASE45678
hector hector1978:BUMPCASE45678
simon sim1984:BUMPCASE3123098
roger roger1985:BUMPCASE3123098
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...