0

I am having a file input.txt which include ~50,000 rows and ~100 column. I want to split is according to matching entry followed by the matching patter. File separator are both space and tab.

input.txt

#information  
#dateofcreation  
#file type
AA  BB  CC DD EE FF GG HH II 
AA  bb  ac aD FF GG hg ad 
DA  ga  Dt pp Ee    FF gg pm    TT
DA  bR  AT GT Gg    FF GG Hb    Yh
NM  gt  Jh GT FF    hb TH KM MM

In the input file there a matching field FF in all the lines followed by the entry matches in some lines. I want to have three output file from this input file

GG.txt

AA  BB  CC DD EE FF GG HH II
AA  bb  ac aD FF GG hg ad
DA  bR  AT GT Gg    FF GG Hb Yh

gg.txt

DA  ga  Dt pp Ee    FF gg pm    TT

hb.txt

NM  gt  Jh GT FF    hb TH KM MM

Thanks.

4
  • Not clear. There's just ONE single EE entry. What is the "matching entry"? Why the trailing "DA" in the second line of GG.txt? Please rephrase.
    – RudiC
    Oct 18, 2018 at 21:10
  • @RudiC My apology. I made a mistake. The matching patter should be FF. I am editing the post. Thanks for pointing it. Oct 18, 2018 at 21:14
  • And the trailing DA?
    – RudiC
    Oct 18, 2018 at 21:31
  • @RudiC Looks like while copying the files, there happen something and DA move. My sincere apology for the confusion. Oct 18, 2018 at 21:35

1 Answer 1

0

Try

awk '
match ($0, /FF *[^ ]* /)        {FN = substr($0, RSTART+3, RLENGTH -4) ".txt"
                                 gsub (/ /, "", FN)
                                 print $0  >  FN
                                }
' file
2
  • Would you mind if I ask you what "RSTART+3, RLENGTH -4"? Oct 18, 2018 at 21:43
  • man awk: match(s,r) Returns the index of the first longest match of regular expression r in string s . . . As a side effect, RSTART is set to the return value. RLENGTH is set to the length of the match or -1 if no match. +3 skips the FF , -4 accounts for that and the trailing space.
    – RudiC
    Oct 18, 2018 at 21:49

You must log in to answer this question.

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