-2

I have a flat file source with no line feed or carriage return. There is five character string, #@#@#, indicating that it is end of line i.e,

abc|def|123#@#@#xyz|tuv|567#@#@#

I need to process this file through informatica, but I believe I cannot set the row delimiter with that five character string.

I need to write a batch script(.bat) to replace all occurences of that string with a line feed character. I have searched a lot, and yet to find any result. Anyone knows how I can handle it? I have seen some solutions using "delims=", though this reads the file line by line. In my case it is only one line due to missing line feed character, hence the solution does not work.

And the output file shoudl look like:

abc|def|123
xyz|tuv|567
MC ND
  • 65,671
  • 6
  • 67
  • 106
yasemin
  • 95
  • 3
  • 10
  • 2
    You should try something on your own first and come back here when you are stuck, clearly describing what you have problems with; otherwise this is a do-my-work-for-me question which is off-topic here! Please read at least the entire [tour page](http://stackoverflow.com/tour) to learn how to use this site! – aschipfl Dec 10 '16 at 20:15
  • Could your input file contain one of the following characters: `"`, `*`, `?`? – aschipfl Dec 10 '16 at 20:16
  • it is a data file, so it is possible to see those characters somewhere in the file. But if you are asking whether the file contains those characters in every field, then my answer would be no, – yasemin Dec 10 '16 at 20:25
  • 1
    In a powershell console enter **(gc InFile.txt -raw) -replace "#@#@#", "`r`n"|set-content OutFile.txt** See answer, formatting eats the escape char. –  Dec 10 '16 at 20:33
  • 2
    In @LotPings' code, there are backticks in `"\`r\`n"` which are difficult to show with Stack Overflow markdown formatting. You could also `(gc infile.txt) -split '#@#@#' > outfile.txt` – rojo Dec 10 '16 at 21:40
  • You may use this regexp: `.replace(/#@#@#/g,"\r\n"))` in the Batch file method given at [this answer](http://stackoverflow.com/questions/40803502/echo-text-with-unix-line-endings-from-a-windows-batch-bat-script/40810929#40810929) – Aacini Dec 11 '16 at 15:49
  • Using [JREPL.BAT](http://www.dostips.com/forum/viewtopic.php?t=6044): `jrepl #@#@# \r\n /x /m /f yourFile.txt /o -` – dbenham Dec 11 '16 at 15:51
  • I have tried both powershell scripts and both work. perfectly fine. Only thing is, I have very big files, and -replace works faster than split, in case anyone would want to know. Thank you all for your help.I really appreciate it. – yasemin Dec 13 '16 at 20:19

2 Answers2

4

In a powershell console enter

(gc InFile.txt -raw) -replace "#@#@#", "`r`n"|set-content OutFile.txt  

Replace InFile.txt , OutFile.txt to fit your needs

2

I do not know why I made things so complicated before -- see the original answer below...

Anyway, the following code snippet should do what you want:

@echo off
setlocal EnableExtensions DisableDelayedExpansion

rem // Define constants here:
set "_FILE=%~1" & rem // (first command line argument)
set "_SEQU=#@#@#"
rem // Define line-break:
(set _LF=^
%= empty line =%
)

rem // Read specified input file:
for /F usebackq^ delims^=^ eol^= %%L in ("%_FILE%") do (
    rem // Store line string:
    set "LINE=%%L"
    setlocal EnableDelayedExpansion
    rem // Replace all sequences by line-breaks:
    echo(!LINE:%_SEQU%=^%_LF%%_LF%!
    endlocal
)

endlocal
exit /B

Original Answer

Here is a pure solution -- see all the explanatory rem remarks:

@echo off
setlocal EnableExtensions DisableDelayedExpansion

rem // Define constants here:
set "_FILE=%~1" & rem // (first command line argument)
set "_SEQU=#@#@#"

rem // Read specified input file:
for /F usebackq^ delims^=^ eol^= %%L in ("%_FILE%") do (
    rem // Store line string:
    set "LINE=%%L"
    setlocal EnableDelayedExpansion
    rem // Double quotation marks temporarily:
    set "LINE=!LINE:"=""!^"
    rem /* Put quotation marks around the entire line string, then replace every
    rem    found sequence by a space in between quotation marks; this results in
    rem    a string in which each proposed output line is placed in between a pair
    rem    of quotation marks and separated from each other by a single space: */
    set LINE="!LINE:%_SEQU%=" "!"
    rem // Provide the resulting string as argument string for a sub-routine:
    call :PROCESS !LINE!
    endlocal
)

endlocal
exit /B


:PROCESS
setlocal DisableDelayedExpansion
rem // Establish a loop over all given (quoted) arguments:
:LOOP
rem // Check current argument against emptiness, meaning that the end is reached:
set ARG=%1
if not defined ARG endlocal & exit /B
rem // Store the argument with the surrounding quotation marks removed:
set "ARG=%~1"
setlocal EnableDelayedExpansion
rem // Reverse doubling of carets done by the sub-routine call:
if defined ARG set "ARG=!ARG:^^=^!"
rem // Reverse doubling of quotation marks:
if defined ARG set "ARG=!ARG:""="!^"
rem // Return the resulting output line string:
echo(!ARG!
endlocal
rem // Move over to next argument:
shift
goto :LOOP
aschipfl
  • 28,946
  • 10
  • 45
  • 77
  • Hi aschipfl. Thanks again for your answer. This updated code is able to read multiple lines up to 8184 character. When file has more character, It doesn't do anything. I understand it is trying to read everything at once. Thank you – yasemin Dec 13 '16 at 16:06
  • You're welcome! The file is read line by line using `for /F`, which is limited to 8191 characters/bytes per line; another limit comes in by the line `set "LINE=%%L"`, which amounts to 8184 bytes, which is 8191 – 4 – 1 – 2, where 4 is the length of the variable name and 1 is the equal-to sign, which are both stored in the environment block, together with the string value, of course; a third limit might also often count — the length of a command line, particularly in case of immediate (`%VAR%`) expansion. Unfortunately, you can avoid these limits only by using another language... – aschipfl Dec 13 '16 at 16:27