1

I have a file A.txt (sep=","):

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
,,,,,,,,,,
*reporting.

And I want to add a pattern from line 3 in the first column of my file after line 13, like that :

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.

The trick is to set " = " as separator for B.txt in order to print the data of A.txt in the $1 of B.txt. I've try stuff like:

awk 'BEGIN{OFS=FS=" = "} NR==3{stuff} } 1' A.txt > B.txt

But i didn't figure it out. Any ideas?

Thanks

2 Answers 2

2

Try:

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

Using your sample input:

$ 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

Multiline format

For those who prefer their commands spread over multiple lines:

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

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

How it works

  • -F' = '

    This sets the field separator to =.

  • NR==3{a=$2}

    For line number three, this saves the second field in variable a.

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

    For line number less than 14, this prints the line unchanged. For the remaining lines, this prints the line with variable a in front of it.

Update: Adding time from lines 14 to all but the last four lines

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

Example input file:

$ 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

Corresponding output:

$ 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
8
  • Thanks for the detail response, it's awesome ! But in the end of my file I have 4 lines ( 2 in my example ) where I should not print the data : Can I do something with tail -n+4 ?
    – nstatam
    Jul 15, 2020 at 7:05
  • @nstatam OK. I'm not sure I know what you're asking but, if you want to cut off (remove) the last 4 lines of output, use head -n-4
    – John1024
    Jul 15, 2020 at 7:15
  • @nstatam Do you want to remove the last 4 lines or leave them unchanged? Is there some distinctive feature of those lines? It looks like that the lines that you want to change have taq in them. Do the last 4 lines lack taq?
    – John1024
    Jul 15, 2020 at 7:18
  • I don't want to cut off, just print $0 on those lines
    – nstatam
    Jul 15, 2020 at 7:19
  • 1
    Thanks it's perfect. Again, thanks for your time
    – nstatam
    Jul 15, 2020 at 8:55
0

A "line agnostic" version

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.

Set FS and OFS to , but split out the date into the array ar on the line where you find UTC and then just insert in as $1 wherever $2=="taq"

1
  • Unfortunately this is not the only line with an UTC pattern and $2 can have several pattern
    – nstatam
    Jul 15, 2020 at 7:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .