Split file when value in column is blank

Hi Experts,

In short : Need to split file when field in column 5 is blank and need to generate two file in which column 5 is blank and other in which column 5 has values along with other rows and column data

My issue is i am not able to get header for column from raw file into new file which is generated by below command.

awk -F"|" -v OFS="|" '{if ($5 == "") print;}' test1.txt

Current output without header

KE|KE_OUTWARD_B2B_OSR_TT_TT_201025_V1.0.txt|30|||Outward|KAR|PO|082017|29AAACT24|INV|TAX|3010482048|2017-11-03||||1|29AAACH27|||M/s HD . LTD||||29||||||9984||Tel Ser|||||0.00|0.00|9.00|45.00|9.00|45.00|||||590.00||||||||||

Expected output should be with header like below

SOUFIER|SOUAME|GLACCODE|DIVISION|SUSION|PR1|PR2|PLADE|RETURNPERIOD|SUPPIN|DYPE|SUYPE|DOMBER|DOTE|ORIENTNUMBER|RIDATE|CRGST|LIBER|CUTIN|UINORON|ORIGIG|CUSTME|CUSTOMERCODE|BILTE|SHIPTE|POS|PODE|SHIMBER|SHTE|FOB|EXY|HSNORSAC|PRODUCTCODE|PRODIPTION|CATEDUCT|UNITONT|QUANTITY|TAUE|INTERATE|INTEEDTOUNT|CENTATE|CENTOUNT|STAATE|STATUNT|CEOREM|CESOREM|CESCIFIC|CESCIFIC|INVE|REVFLAG|TCSFLAG|ECOMGSTIN|ITCFLAG|REASONBITNOTE|ACCOUNTMBER|ACCOUNTINGVOUCHERDATE|USERDEFI|USERD|USE
KEN|KE_OUT_B2B_OSR_TT_TT_201025_V1.0.txt|22|||Outward|KAR|PO|082017|29A38A1ZP|INV|TA|3010048|2017-11 03||||1|29AAAZW|||M/s HD. LTD||||29||||||9984||Tel Ser|||||0.00|0.00|9.00|45.00|9.00|45.00|||||590.00||||||||||

Please become accustomed to provide decent context info of your problem.

It is always helpful to carefully and detailedly phrase a request, and to support it with system info like OS and shell, related environment (variables, options), preferred tools, adequate (representative) sample input and desired output data and the logics connecting the two including your own attempts at a solution, and, if existent, system (error) messages verbatim, to avoid ambiguities and keep people from guessing.

Try NR == 1 to print the header to either file.

Hi as7951,
Expanding greatly on what RudiC said...

Instead of telling us "in short" what you're trying to do, please tell us exactly what you're trying to do in full.

It should be obvious that there is nothing in the code you showed us above that will print a header line. What isn't know includes:

  1. Does the header line you want in the output file appear as the 1st line in your input file, or does the header need to be created manually?
  2. Should that header line always appear in your output file, or should it only appear if the another output line is written to that file?
  3. If that file is created will it only contain two lines (the header and the line shown above), or might other errors also be logged into that file?
  4. Is the awk code shown above your entire awk script, or is it another abbreviation that would be likely to significantly alter the suggestions we might make to address your problem?
  5. What operating system are you using?

The more you tell us about what you're trying to do and the environment in which you're working, the more likely we are to be able to help you with suggestions that might do what you want in your environment. The less you tell us, the more likely it is that we won't understand what you want and will make suggestions that won't do what you want or won't work in your environment.

Please help us help you by giving us the details we need to understand what you're trying to do.

And, please consider this question: Who on earth should care MORE for your problem than you do?

Would csplit help here? You can put in a regular expression to split the file on.

Have a read of the manual page and see if that does what you need, then have a go and see if it does what you need. If it's close, but not right or you can't get it working at all, post the details and your efforts so we can see about helping.

Kind regards,
Robin