как напечатать шаблон из строки в столбце в awk

Пробовал с помощью приведенной ниже команды и работал нормально

команда

pacmd list-modules|awk '/index: [0-9]/||/name: <.*>/||/argument: <.*?>/{print $0}' 
1
15.07.2020, 09:40
2 ответа

Попробуйте:

awk -F' = ' 'NR==3{a=$2} {if(NR<14)print; else print a $0}' A.txt

Использование вашего образца ввода:

$ awk -F' = ' 'NR==3{a=$2} {if(NR<14)print; else print a $0}' A.txt
kit
Software Version =
Date And Time of Export = 07/02/2020 13:44:11 UTC
Experiment Name =
Instrument Software Version =
Instrument Type = CFX
Instrument Serial Number =
Run Start Date =
Run End Date =
Run Operator =
Batch Status = VALID
Method = Novaprime
Date And Time of Export,Batch ID,Sample Name,Well,Sample Type,Status,Interpretive Result,Action*,Curve analysis
07/02/2020 13:44:11 UTC,taq,205920777.1,A01,Unkn-01
07/02/2020 13:44:11 UTC,taq,neg5,A02,Unkn-09

Многострочный формат

Для тех, кто предпочитает, чтобы их команды располагались на нескольких строках:

awk -F' = ' '
    NR==3{
        a=$2
    }

    {
        if(NR<14)
            print
        else
            print a $0
    }
    ' A.txt

Как это работает

  • -F' = '

    Это устанавливает разделитель полей на =.

  • NR==3{a=$2}

    Для строки номер три это сохраняет второе поле в переменной a.

  • if(NR<14)print; else print a $0

    Если номер строки меньше 14, строка печатается без изменений. Для остальных строк печатается строка с переменной aперед ней.

Обновление :Добавление времени из строк 14 ко всем строкам, кроме последних четырех

awk -F' = ' 'NR==3{t=$2} NR<14{print;next} NR>17{print t d} {d=c;c=b;b=a;a=$0} END{print d ORS c ORS b ORS a}' A.txt

Пример входного файла:

$ cat A.txt
kit
Software Version =
Date And Time of Export = 07/02/2020 13:44:11 UTC
Experiment Name =
Instrument Software Version =
Instrument Type = CFX
Instrument Serial Number =
Run Start Date =
Run End Date =
Run Operator =
Batch Status = VALID
Method = Novaprime
Date And Time of Export,Batch ID,Sample Name,Well,Sample Type,Status,Interpretive Result,Action*,Curve analysis
,taq,205920777.1,A01,Unkn-01
,taq,neg5,A02,Unkn-09
end1
end2
end3
end4

Соответствующий выход:

$ awk -F' = ' 'NR==3{t=$2} NR<14{print;next} NR>17{print t d} {d=c;c=b;b=a;a=$0} END{print d ORS c ORS b ORS a}' A.txt
kit
Software Version =
Date And Time of Export = 07/02/2020 13:44:11 UTC
Experiment Name =
Instrument Software Version =
Instrument Type = CFX
Instrument Serial Number =
Run Start Date =
Run End Date =
Run Operator =
Batch Status = VALID
Method = Novaprime
Date And Time of Export,Batch ID,Sample Name,Well,Sample Type,Status,Interpretive Result,Action*,Curve analysis
07/02/2020 13:44:11 UTC,taq,205920777.1,A01,Unkn-01
07/02/2020 13:44:11 UTC,taq,neg5,A02,Unkn-09
end1
end2
end3
end4
2
18.03.2021, 23:19

Версия, не зависящая от линии

awk -F, -v OFS="," '$0~/UTC/{split($0,ar,"= ")}$2=="taq"{$1=ar[2]}1' file

kit
Software Version =
Date And Time of Export = 07/02/2020 13:44:11 UTC
Experiment Name =
Instrument Software Version =
Instrument Type = CFX
Instrument Serial Number =
Run Start Date =
Run End Date =
Run Operator =
Batch Status = VALID
Method = Novaprime
Date And Time of Export,Batch ID,Sample Name,Well,Sample Type,Status,Interpretive Result,Action*,Curve analysis
07/02/2020 13:44:11 UTC,taq,205920777.1,A01,Unkn-01
07/02/2020 13:44:11 UTC,taq,neg5,A02,Unkn-09
,,,,,,,,,,
*reporting.

Установите для FSи OFSзначение ,, но разделите дату на массив arв строке, где вы найдете UTC, а затем просто вставьте как $1везде, где$2=="taq"

0
18.03.2021, 23:19

Теги

Похожие вопросы