-1

I have a template file A.txt (sep = \t ):

File Name   novaprime-ct043904-TB_2140.pcrd
Created By User admin
Notes   
ID  
Run Started 09/17/2020 01:19:12 UTC
Run Ended   09/17/2020 03:12:22 UTC
Sample Vol  30
Lid Temp    105
Protocol File Name  Cll Novaprime.prcl
Plate Setup File Name   CFX-TB_2140-20200916.pltd
Base Serial Number  CT043904

And a second file B.txt (sep = \t):

Cycle   Well    Value   Target  Content
1   A1  5.07368111264623    EC  Unkn-01
1   A1  3.06982862746599    FT  Unkn-09
1   A1  2.46545646544623    EC  Unkn-01

And I want to add an info from the 10th line of A.txt (what's in bold) to a new column in B.txt: Plate Setup File Name CFX-TB_2140-20200916.pltd

In order to have C.txt(sep = \t):

Cycle   Well    Value   Target  Content Plate
1   A1  5.07368111264623    EC  Unkn-01 TB_2140
1   A1  3.06982862746599    FT  Unkn-09 TB_2140
1   A1  2.46545646544623    EC  Unkn-01 TB_2140

To do that I try various awk command using "-" as a separator but it didn't work. Do you have an idea how to do that ? Thanks

  • You could mention what you tried with awk so we can try to understand where the problem is. – AdminBee Sep 17 at 13:06
2
awk 'BEGIN{FS=OFS="\t"}
     NR==FNR {if (/^Plate Setup File Name/) {split($2,a,"-");id=a[2]};next}
     {if (FNR==1) $6="Plate"; else $6=id} 1' A.txt B.txt > C.txt

This will scan for a line "Plate Setup File Name" in A.txt, separate the value part into fields on -, and store the "Plate" part. When processing B.txt, it substitutes either a suitable header entry, or the plate ID.

| improve this answer | |
  • Thanks. My problem washow to store the information. Now thanks to you I know how to do that – nstatam Sep 17 at 13:13
  • Consider writing that as 'NR==FNR { if (/^Plate Setup File Name/) {split($2,a,"-");id=a[2]} next} {$6= (FNR==1 ? "Plate" : id} 1' as that'd get rid of both the redundant (e.g. the next and \ in your code are doing nothing) and duplicate (referencing $6 twice) code and improve the efficiency (removing the test for NR>FNR). You could also use BEGIN{FS=OFS="\t"} so you don't specify the same value (\t) in 2 separate statements. – Ed Morton Sep 17 at 13:20
  • 1
    @EdMorton Yeah, the next was actually from an attempt to make it shorter in the way you describe it; I somehow didn't finish it, must really get some coffee here ... As for your other comments: That would make the code more compact, but as you may have noticed, I often try to make it easier to read rather than "golfing" it. I do certainly agree that setting FS and OFS in the BEGIN section is more efficient, though. – AdminBee Sep 17 at 13:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.