1

I download a text file using CPYTOIMPF with record delimiter as CRLF. As requirement the delimiter as CRLF is correct for all lines except last line where there should not be any delimiter. Since this command auto adds delimiter to all lines how can i control this for last line ? there should not be any new line in text file at the end.

CPYTOIMPF FROMFILE(TESTFILE *FIRST) TOSTMF('/TEST.TXT') MBROPT(*REPLACE) FROMCCSID(874) STMFCODPAG(874) RCDDLM(*CRLF) STRDLM(*NONE) RMVBLANK(*TRAILING) FLDDLM(',') ORDERBY(*NONE) ADDCOLNAM(*NONE)

  • "there should not be any new line in text file at the end" - why not? A CRLF (or LF) with an EOL in the last "record" may not be processed correctly in all environments, there definitely _should_ be a [CR]LF there – Simon Sobisch Jun 05 '20 at 04:38
  • @SimonSobisch Yes normal scenario the EOL should exist. But as per this requirement the client system support only with last line without EOL – Nat Taggart Jun 05 '20 at 04:58
  • I am not clear how to call the API from CL since I usually do it from RPG, but the [ftruncate](https://www.ibm.com/support/knowledgecenter/ssw_ibm_i_73/apis/ftruncat.htm) function in service file QP0LLIB1 should be able to do this. Pass it current file length-2 and it should chop off the last two characters. – Mike Jun 05 '20 at 11:42
  • Someone who's good with Unix/Linux commands can probably recommend something you can use from QSH or PASE. It would just be a one-liner. For sure `sed` is available. What would be even more convenient is the `truncate` command, but I don't think IBM i comes with that. Also, if you are comfortable with Perl, Python, or Node.js, you could very easily install one of those and use that (if `yum` is installed, then you already have Perl and Python 2; Node.js and Python 3 are an additional but very simple step). – John Y Jun 05 '20 at 17:00
  • @JohnY Yes i have tried sed. using sed i'm able to remove *CR but sed adds *LF at the end. So i'm back to square one. sed '$s/\r\n/ /' SAMP1.txt > RESULT1.TXT – Nat Taggart Jun 08 '20 at 07:33

1 Answers1

0

The truncate command is available from the coreutils-gnu package via yum. This makes it easy to adjust the answer here and subtract 2 bytes for the CRLF from the end of the file:

truncate -s-2 /TEST.TXT
Kevin Adler
  • 641
  • 5
  • 16