VIM search and replace with line breaks in both the target and replacement text


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers VIM search and replace with line breaks in both the target and replacement text
# 1  
Old 08-02-2011
VIM search and replace with line breaks in both the target and replacement text

Hi,

Ive spent ages trying to find an explanation for how to do this on the web, but now feel like I'm Smilie

I would like to change each occurence (there are many within my script) of the following:

Quote:
If(((me==1.and.(i==11.and.j==23))&
&.or.(me==2.and.(i==3.and.j==35)))&
to

Quote:
If(((me==3.and.(i==12.and.j==8))&
&.or.(me==4.and.(i==8.and.j==55)))&
&.or.(me==5.and.(i==2.and.j==2)))&
in Vim. I know how to search and replace when it is just single lines involved (:%s/find_text/replace_text/g etc), but am struggling with the line breaks. Any help is much appreciated.

Cheers, Andy

Last edited by blueade7; 08-02-2011 at 08:28 AM..
# 2  
Old 08-02-2011
You can use newline chars in s command in pattern part as "\n" and in substitute part as "\r". You should backspace & in substitute part. This awful expression works:
Code:
:%s/If(((me==1.and.(i==11.and.j==23))&\n&.or.(me==2.and.(i==3.and.j==35)))&\n/If
(((me==3.and.(i==12.and.j==8))\&\r\&.or.(me==4.and.(i==8.and.j==55)))\&\r\&.or.(
me==5.and.(i==2.and.j==2)))\&\r/g

# 3  
Old 08-02-2011
Thats great, thanks v much.

Andy

Last edited by blueade7; 08-02-2011 at 11:18 AM..
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to add line breaks to perl command with large text in single quotes?

Below code extracts multiple field values from XML into array and prints all in one line. perl -nle '@r=/(?: jndiName| authDataAlias| value| minConnections| maxConnections| connectionTimeout| name)="(+)/g and print join ",",$ENV{tIPnSCOPE},$ENV{pr ovider},$ENV{impClassName},@r' server.xml ... (4 Replies)
Discussion started by: kchinnam
4 Replies

2. UNIX for Dummies Questions & Answers

Convert UNIX text file in Windows to recognize line breaks

Hi all, I have some text files that I prepared in vi some time ago, and now I want to open and edit them with Windows Notepad. I don't have a Unix terminal at the moment so I need to do the conversion in Windows. Is there a way to do this? Or just reinsert thousands of line breaks again :eek: ? (2 Replies)
Discussion started by: frys_hp
2 Replies

3. Windows & DOS: Issues & Discussions

Convert UNIX text file in Windows to recognize line breaks

Hmmm I think I found the correct subforum to ask my question... I have some text files that I prepared in vi some time ago, and now I want to open and edit them with Windows Notepad. I don't have a Unix terminal at the moment so I need to do the conversion in Windows. Is there a way to do this?... (1 Reply)
Discussion started by: frys_hp
1 Replies

4. Shell Programming and Scripting

Search and Replace a text if the line contains a pattern

My text file looks like below . . . abcdefghi jklmnop $Bad_ptrq_GTS=rcrd_ip.txt $Bad_abcd_REJ=rcrd_op.txt ghijklm $Bad_abcd_TYHS=rcrd_op.txt abcgd abcdefghi jklmnop $Bad_ptrq_GTS=rcrd_ip.txt (2 Replies)
Discussion started by: machomaddy
2 Replies

5. Shell Programming and Scripting

Search text and replace line next to it

I have a file which requires modification via a shell script. Need to do the following: 0. take input from user for new text. 1. search for a keyword in the file. 2. replace the line next to this to this keyword with user supplied input. for e.g., my file has the following text: (some... (7 Replies)
Discussion started by: chingupt
7 Replies

6. Shell Programming and Scripting

How to fix line breaks format text for huge files?

Hi, I need to correct line breaks for huge files (more than 1MM records in a file) and then format it properly. Except the header and trailer, each record starts with 'D'. Requirement:Scan the whole file except the header and trailer records and see if any of the records start with... (19 Replies)
Discussion started by: kikionline
19 Replies

7. Shell Programming and Scripting

perl search and replace - search in first line and replance in 2nd line

Dear All, i want to search particular string and want to replance next line value. following is the test file. search string is tmp,??? ,10:1 "???" may contain any 3 character it should remain the same and next line replace with ,10:50 tmp,123 --- if match tmp,??? then... (3 Replies)
Discussion started by: arvindng
3 Replies

8. UNIX for Dummies Questions & Answers

VIM: replace a character in the middle of line

exmaple, i need to replace the number "5" from all lines below with "X"? What is the useful vim command that i can apply for.. ddpadsgg506xghssuyj ddpadsgag546xghssuys ddsadsgaag596xghssuy_te ddsadsgag506xghssuy_pe ddsadsgagc526xghssuys ddsads506ighssuys ddsadsgag506pghssuyk (1 Reply)
Discussion started by: 793589
1 Replies

9. Shell Programming and Scripting

Perl: Search for string on line then search and replace text

Hi All, I have a file that I need to be able to find a pattern match on a line, search that line for a text pattern, and replace that text. An example of 4 lines in my file is: 1. MatchText_randomNumberOfText moreData ReplaceMe moreData 2. MatchText_randomNumberOfText moreData moreData... (4 Replies)
Discussion started by: Crypto
4 Replies

10. Shell Programming and Scripting

Search and replace multi-line text in files

Hello I need to search for a mult-line text in a file exfile1 and replace that text with another text. The text to search for is in exfile2 and the replacement text is in exfile3. I work with kornshell under AIX and need to do this with a lot of files. (the file type is postscript and they need... (10 Replies)
Discussion started by: marz
10 Replies
Login or Register to Ask a Question

Featured Tech Videos