Insert a break page after certain string using SED

Hi:

I have 2 files: teststring.txt and a tempfile.txt

teststring file contains:

s/Primary Ins./\n1/g 

I'm trying to search for "Primary Ins." string in tempfile. For every "Primary Ins." string that is found, a new line is inserted and put in number 1. Then, write out the newfile everything in tempfile + the number 1.

My testrun.bat file is follow:

     @ECHO OFF                          
       SETLOCAL  
    
    SET SED_PATH="C:\Program Files (x86)\GnuWin32\bin"
    SET SED_PROGRAM=sed.exe
    SET SED_PATH_PROGRAM=%SED_PATH%\%SED_PROGRAM%
    
    sed -f C:\test\teststring.txt C:\test\tempfile.txt > C:\test\newfile.txt

Although a newfile.txt file is created, it's completely empty. Help please!

Is sed actually in your PATH?

Try c:\path\to\sed.exe instead of just 'sed'

A \n is just a newline by the way, not a page break. Do you mean form feed, \f ?

Corona688:

sed.exe can be found at C:\Program Files (x86)\GnuWin32\bin
This is where I defined on SED_PATH and SED_PROGRAM is calling sed.exe. Is this wrong? If so, how should I define it?

What I want to do is whenever "Primary Ins." is founded, a new line will be inserted along with a #1.

Thanks for all your help and looking into this.

---------- Post updated 12-06-11 at 12:00 PM ---------- Previous update was 12-05-11 at 06:10 PM ----------

I figured out the sed part. However, what I'm trying to do is to put in a 1 AFTER anyline that has 'Primary Ins.'

This code below gives me the opposite. It puts in a 1 BEFORE the line that 'Primary Ins.' is found. Help please!

s/Primary Ins./1\
Primary Ins./g

Yes, SED_PATH is meaningless, the variable PATH that I mentioned is what you need to change. Or you could just try calling sed with the absolute path like I suggested.

Hmm.

's/^\(Primary Ins\..*\)$/\1\n\1\n/'

There is no '1' inserted into the next line. It just rewrite the whole file as is. :frowning:

Trying to test it here. It's highly annoying that the Windows GNU sed I have doesn't take \n, because windows CMD can't do multi-line strings! :wall:

no luck :frowning: the whole original file is produced, no 1 is added. Ideally, this is what I'm looking for in the newfile.txt

Primary Ins.: MEDICARE B DMERC Secondary Ins.: CONTINENTAL LIFE INS
1

CMD shell is annoying. You get no quoting at all, so every program handles quotes a different way. You DO get splitting. Every program decides what's really been split or not a different way. Best to put code into a file if you can, for the program to read unmangled.

If you can get or install awk, put this in a text file:

/^Primary Ins\./ { $0=$0 "\n" "1" } 1

Then do this:

awk -f ins.txt input.txt > output.txt

If it complains about 'unknown token', run dos2unix on ins.txt