Find a string and place two blank lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find a string and place two blank lines
# 1  
Old 09-15-2009
Find a string and place two blank lines

Hi friends,

I am looking for a line to find a particular string in my file and once found then replace with 2-3 blank lines before the string

Example:
Code:
 
aaa 11 bbb
1
2
3
 
aaa 22 bbb
4
5
6

Output
Code:
 
 
aaa 11 bbb
1
2
3
 
 
 
aaa 22 bbb
4
5
6

Thanks in advance
# 2  
Old 09-15-2009
Is this what you are after?

Code:
$ cat file
aaa 11 bbb
1
2
3

aaa 22 bbb
4
5
6

aaa 33 bbb
1
2
3

Code:
$ awk -v RS="" -v ORS="\n\n" '/aaa 22 bbb/{print "\n\n" $0;next}{print}' file
aaa 11 bbb
1
2
3



aaa 22 bbb
4
5
6

aaa 33 bbb
1
2
3

# 3  
Old 09-15-2009
I am getting below error:
Code:
omcadmin>awk -v RS="" -v ORS="\n\n" '/aaa 22 bbb/{print "\n\n" $0;next}{print}' file
awk: syntax error near line 1
awk: bailing out near line 1



---------- Post updated at 05:59 PM ---------- Previous update was at 05:55 PM ----------

Sorry that worked..
But i need for all lines starting with aaa and ending with bbb
I tried this but not working
Code:
nawk -v RS="" -v ORS="\n\n" '/^aaa.*bbb$/{print "\n\n" $0;next}{print}' file

# 4  
Old 09-15-2009
Tested with GNUawk, original-awk and mawk. All successfully.

Try:
Code:
awk 'BEGIN{RS="";ORS="\n\n"} /aaa 22 bbb/{print "\n\n" $0;next}{print}' file



---------- Post updated at 02:37 PM ---------- Previous update was at 02:33 PM ----------
Quote:
Originally Posted by shaliniyadav
But i need for all lines starting with aaa and ending with bbb
Ok. Then, simply try

Code:
awk '/^aaa.+bbb$/{print "\n\n" $0;next}{print}' file

# 5  
Old 09-15-2009
Thanks a LOT Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

To check Blank Lines, Blank Records and Junk Characters in a File

Hi All Need Help I have a file with the below format (ABC.TXT) : ®¿¿ABCDHEJJSJJ|XCBJSKK01|M|7348974982790 HDFLJDKJSKJ|KJALKSD02|M|7378439274898 KJHSAJKHHJJ|LJDSAJKK03|F|9898982039999 (cont......) I need to write a script where it will check for : blank lines (between rows,before... (6 Replies)
Discussion started by: chatwithsaurav
6 Replies

2. Shell Programming and Scripting

Find a string and print all lines upto another string

Ok I would like to do the following file test contains the following lines. between the lines ABC there may be any amount of lines up to the next ABC entry. I want to grep for the filename.txt entry and print the lines in between (and including that line) up to and including the last line... (3 Replies)
Discussion started by: revaroo
3 Replies

3. Shell Programming and Scripting

Find regex, place on individual lines and insert blank line before

Hello, I have a file that I want to be able to insert a new line before every instance of a regex. I can get it to do this for each line that contains the regex, but not for each instance. Contents of infile: Test this 1... Test this 2... Test this 3... Test this 4... Test this... (2 Replies)
Discussion started by: deneuve01
2 Replies

4. Shell Programming and Scripting

Insert a string instead of blank lines

how can i insert a string sush as "###" instead of blank lines in a file? i try this code but it doesn't work! awk 'NF<1 {$1=="###" ; print$0}' in_file > out_file (13 Replies)
Discussion started by: oreka18
13 Replies

5. Shell Programming and Scripting

Inserting blank lines after string change

My input data looks like this ... -150 120 8 -150 122 7 -150 124 11 -150 126 8 -150 128 19 -150 130 13 -150 132 26 -150 134 38 -150 136 45 -150 138 62 -150 140 75 -150 142 110 -150 144 139 -150 146 138 -150 148 158 -150 150 173 -150 152 217 (5 Replies)
Discussion started by: chrisjorg
5 Replies

6. Shell Programming and Scripting

Delete blank lines, if blank lines are more than one using shell

Hi, Consider a file named "testfile" The contents of file are as below first line added for test second line added for test third line added for test fourth line added for test fifth line added for test (5 Replies)
Discussion started by: anil8103
5 Replies

7. Shell Programming and Scripting

Awk - find string, search lines below string for other strings

What's the easiest way to search a file for a specific string and then look for other instances after that? I want to search for all Virtual Hosts and print out the Server Name and Document Root (if it has that info), while discarding the rest of the info. Basically my file looks like this: ...... (6 Replies)
Discussion started by: Mbohmer
6 Replies

8. Shell Programming and Scripting

Find a string in textfile, erase $num lines after that string

I have a textfile containing text similar to the following pattern: STRING1 UNIQUE_STRING1 STRING2 STRING3 STRING4 STRING5 STRING1 UNIQUE_STRING2 STRING2 STRING3 STRING4 STRING5 STRING1 UNIQUE_STRING3 STRING2 STRING3 (6 Replies)
Discussion started by: ilcsfe
6 Replies

9. Shell Programming and Scripting

Print lines after the search string until blank line is found

All I want is to look for the pattern in the file...If I found it at # places... I want print lines after those pattern(line) until I find a blank line. Log EXAMPLE : MT:Exception caught The following Numbers were affected: 1234 2345 2346 Error java.lang.InternalError:... (3 Replies)
Discussion started by: prash184u
3 Replies

10. Shell Programming and Scripting

Unix help to find blank lines in a file and print numbers on that line

Hi, I would like to know how to solve one of my problems using expert unix commands. I have a file with occasional blank lines; for example; dertu frthu fghtu frtty frtgy frgtui frgtu ghrye frhutp frjuf I need to edit the file so that the file looks like this; (10 Replies)
Discussion started by: Lucky Ali
10 Replies
Login or Register to Ask a Question