Insert a break page after certain string using SED


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Insert a break page after certain string using SED
# 1  
Old 12-05-2011
Insert a break page after certain string using SED

Hi:

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

teststring file contains:
Code:
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:
Code:
     @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!

Moderator's Comments:
Mod Comment How to use code tags

Last edited by Franklin52; 12-07-2011 at 05:53 AM.. Reason: Please use code tags for code and data samples, thank you
# 2  
Old 12-05-2011
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 ?
# 3  
Old 12-06-2011
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
# 4  
Old 12-06-2011
Quote:
Originally Posted by newbeee
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?
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.
Quote:
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
Hmm.
Code:
's/^\(Primary Ins\..*\)$/\1\n\1\n/'

# 5  
Old 12-06-2011
There is no '1' inserted into the next line. It just rewrite the whole file as is. Smilie
# 6  
Old 12-06-2011
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! Smilie
# 7  
Old 12-06-2011
no luck Smilie 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
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

[sed]: syntax to insert N spaces in front of a string

Dear all, I would like to insert N blankspaces in front of a string using sed command To give an example (N=10), I tried that code: $ echo "abcd" | sed 's/^/ \{10,\}&/' but I failed, by obtaining that result: {10,}abcd Any help would be greatly appreciated, Thanks in advance,... (18 Replies)
Discussion started by: dae
18 Replies

2. Shell Programming and Scripting

How to insert file contents after nth occurrence of a string using sed?

Hi, I would like to know how, using sed, be able to insert contents of file2 in file1 after say the second occurrence of a given string? e.g. > cat file1 banana apple orange apple banana pear tangerine apple > cat file2 I don't like apples What would be the sed command to insert... (5 Replies)
Discussion started by: dimocn
5 Replies

3. Shell Programming and Scripting

Page Break in a file for printing

Hi, We have 1lac records in source file and unix script will genarate around 1000 files. From target location the files are taking for printing on physical papers. the page size limitation : 256 Lines Can you please tell me how to insert the page break in a flat file for printer. (5 Replies)
Discussion started by: koti_rama
5 Replies

4. Shell Programming and Scripting

Page Break in large file

Hi, The shell script inserting the millions of rows into target flat file system and handling the line number for each line. We need a page break line after every 10,000 lines. is there any command to insert a page break line into target file. (3 Replies)
Discussion started by: koti_rama
3 Replies

5. Shell Programming and Scripting

help - sed - insert space between string of form XxxAxxBcx, without replacing the pattern

If the string is of the pattern XxxXyzAbc... The expected out put from sed has to be Xxx Xyz Abc ... eg: if the string is QcfEfQfs, then the expected output is Qcf Ef Efs. If i try to substitute the pattern with space then the sed will replace the character or pattern with space,... (1 Reply)
Discussion started by: frozensmilz
1 Replies

6. Shell Programming and Scripting

Insert line break

Dear All, thanks in advance input file 410530AAANNNNNAAA410530JJJJJJYYYY410530PPPPPAAAAAA........... I want output like 410530AAANNNNNAAA 410530JJJJJJYYYY 410530PPPPPAAAAAA Thanks (10 Replies)
Discussion started by: The_Archer
10 Replies

7. Shell Programming and Scripting

help on page break

Hi, i have a file say samp.s which has 123 a b c d 123 e f g h 123 i j k l 123 m n o p 234 a b c d 234 e f g h 234 i j k l the first 3 characters in each line are considered the key values i have one more file temp.txt which has 123 234 i want to have a page break in... (5 Replies)
Discussion started by: Sheema
5 Replies

8. Shell Programming and Scripting

Page Break with AWK or SED

Hello All, I am new to unix scripting, I have an urgent issue Is it possible to do a group by on the last column and then a page break on the text file. For example I have a text file, below is the example data STRT 2154081~VA ~23606 ~TM14~8506~1485 STRT 2130893~VA ~23602 ~TM15~8602~1586... (4 Replies)
Discussion started by: udaybo
4 Replies

9. UNIX for Dummies Questions & Answers

Can I use sed to insert a string which has colon

Hi, all, I wonder if I can use sed to insert a string which has a colon. I have a txt file a.txt like the following TRAIN/DR1/FCJF0/SI1027.MFC TRAIN/DR1/FCJF0/SI1657.MFC I want to insert a string C:/TIMIT/TIMIT at the begining of each line. I use the commond: TIM=C\:/TIMIT/TIMIT... (2 Replies)
Discussion started by: Jenny.palmy
2 Replies

10. Shell Programming and Scripting

how to insert line break + string in vi (search & replace )

Hello all i have big test file that has allot of structure text something like this : <foo1 *.html> <blah action> somthing 1 somthing 2 </blah> </foo1 > now i will like to insert 2 more lines of text below the <blah action> so it will be like : <foo1... (1 Reply)
Discussion started by: umen
1 Replies
Login or Register to Ask a Question