0

For example, I have a line

2054721111*2054721111*2054721111*2054721111

Now, I want to replace "*" character as a new line.

What if I want my output to be like this using batch file?

2054721111*
2054721111*
2054721111*
2054721111
Ross Ridge
  • 38,414
  • 7
  • 81
  • 112
PeterS
  • 724
  • 2
  • 15
  • 31
  • Give Sublime Text 3 a try. You can use get multiple cursors at each delimiter and then separate to multiple lines or rejoin them. You can create your own plugin if the existing functionality isn't what you need. – Chris Barlow Oct 10 '14 at 06:52
  • @Chris Barlow Sorry but I dint get what you mean :) I am new with batch programming. – PeterS Oct 10 '14 at 06:54
  • btw I want to implement this in batch file without using any plugins. Is it possible? – PeterS Oct 10 '14 at 06:55
  • Do you need a batch file to do it or do you need to know how to do it with Notepad++? – MC ND Oct 10 '14 at 06:57
  • You mentioned notepad++. I misunderstood your question. – Chris Barlow Oct 10 '14 at 06:58
  • I just mentioned the example equivalent of my question :) – PeterS Oct 10 '14 at 07:05
  • Please learn to use appropriate tags, PeterS. Not only will it help prevent people giving you unhelpful answers, using correct tags make its more likely someone who can help you will see your question. In particular none of your many recent questions has been about the MS-DOS operating system, instead they've all been about Windows. Most people ignore posts tagged with [tag:msdos] and [tag:dos] because MS-DOS is a long obsolete OS that practically no one uses anymore. – Ross Ridge Oct 10 '14 at 15:05

2 Answers2

1
@echo off
    setlocal
    set "data=2054721111*2054721111*2054721111*2054721111"

    Rem Option 1 - first count how many elements there are in the string
    Rem            and then loop over them
    setlocal enableextensions disabledelayedexpansion
    for /f %%n in (
        'cmd /q /u /c "echo(%data%"^|find /c "*"'
    ) do for /l %%i in (0 1 %%n) do (
        setlocal enabledelayedexpansion
        for /f "tokens=1,* delims=*" %%a in ("!data!") do ( 
            endlocal
            set "data=%%b"
            if defined data (echo(%%a*) else (echo(%%a)
        )
    )
    endlocal

    Rem Option 2 - without counting the number of elements
    setlocal enableextensions disabledelayedexpansion
    cmd /q /v:on /e:on /c "for /l %%l in () do if not defined data (exit) else for /f "tokens=1,* delims=*" %%a in ("!data!") do (set "data=%%b" & if defined data (echo %%a*) else echo %%a)"
    endlocal

Both codes show the same idea: use * as a delimiter in a for /f loop, output the first token and save the rest of the line as the input variable. Keep processing until all the tokens has been echoed.

First block of code will not have any problem with exclamations in the data (in the case they could exist), but the second one can fail

MC ND
  • 69,615
  • 8
  • 84
  • 126
  • What if I want a file to be converted? For example I have a file file.txt containing "2054721111*2054721111*2054721111*2054721111" that I want to replace with linefeed? – PeterS Oct 10 '14 at 11:43
  • @PeterS, See [here](http://stackoverflow.com/a/16735079/2861476). dbenhan wrote a nice batch file to do this kind of operations. You will only need to use `type inputfile.txt | repl.bat "\*" "*\n" X > outputfile.txt` – MC ND Oct 10 '14 at 11:51
  • Wow thanks for this I got it working now. One last question. For example I have list of characters in a file and I want those characters to be replace on a file. For example I have delimeter.txt file that contains * ! ^ What if I want to find these delimeter and replace with *\n, !\n and ^\n in a file? I already have a code like this 'for /f "delims=" %%s in (delimeter.txt) do ( Type c:\aaa\%%a | repl.bat "\%%s" "%%s\n" X > c:\aaa\%%a' But It only reads last character – PeterS Oct 10 '14 at 12:56
  • @PeterS, `set /p "delimiters=" < "delimiters.txt"` , `type inputfile.txt | repl.bat "([%delimiters: =%])" "$1\n" X` but be sure the caret is not the first character in the `delimiters.txt` file – MC ND Oct 10 '14 at 15:56
  • I tried to use this in a large file. Ive noticed that when replacing many lines of file, the files were chunked. Do we have limits on this? – PeterS Oct 13 '14 at 10:28
  • @PeterS, just to test, i have tried with a 1.3 million 2500 character records TSV file, to replace tabs into new lines to generate a 19 million records file. And no, no problems. – MC ND Oct 13 '14 at 11:26
  • @PeterS, in your case, what does chunked exactly mean? Lines are cut, file are trimmed, are there missing lines, ...? – MC ND Oct 13 '14 at 11:30
  • I mean files are cut. FOr example I have a file contains 1000 lines to 400 lines. Someting like that – PeterS Oct 13 '14 at 12:04
  • @PeterS, Those files are not even close to what i was trying. It seems more a problem with the command line. Some of your delimiters have special meanings both in batch files and in regular expressions. Try to determine when it fails (pattern vs line content). And if it is not obvious, start a new question. The comment box is really limited and narrow for this. – MC ND Oct 13 '14 at 12:13
0

With Notepad++, just do that:

Ctrl-F, Select Replace tag then:

Find what: \*
Replace with: \n

Clic on Replace all.

Toto
  • 89,455
  • 62
  • 89
  • 125