Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

splitting into multiple files (AGAIN) - saw the previous thread

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Hi. I am very new to awk, and would like some help on this. I have read the previous threads, and couldn't find the references, and didn't understand any of the solutions. Could someone please tell me how to code awk to split a data file that contains several logical files into several physical files (based on "HREC**" and "TREC**" in the first 6 characters)? The output files need to be dynamically named.

Can this be done all in one awk statement? Should I code an awk program?

Here is what I tried so far:
***********************************************
Code:
awk -f splitfile.awk $testfile

splitfile.awk
Code:
 BEGIN { num = 0 testoutfile = "testoutfile" }
    /'^HREC\*\*'/,/'^TREC\*\*'/
       {num +=1
        print  $0  > $testoutfile$num }
***********************************************
This does not work. If I take away the "
Code:
> $testoutfile$num
", it displays the entire file, which seems correct. How do I split it??? How do I output each logical file to a separate file?

Thanks for anyone's help. Please try and explain the solution and code to me in clear English. Please do not refer me to another thread. I'm not good enough!

Gloria

 
Something like this may work for you, depending on the exact format of your input file.
Code:
/^[HT]REC\*\*/{
  if (NR>1) close(fn);
  fn = "testoutfile" ++num
}
{print  > fn }
CaKiwi
 
Thank you so much!!!!! The only thing that didn't quite work was the file name suffix ++num, which worked when I changed it to num++.

Gloria
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top