How to identify exact text and then add a blank line above it using sed?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to identify exact text and then add a blank line above it using sed?
# 1  
Old 06-26-2015
How to identify exact text and then add a blank line above it using sed?

I need to identify the exact text of San Antonio Generator Running in the output my script which lands to a text file. Once SED finds the specific text, I need it to insert one line above the matched text.

Here is what I have so far that isn't working all that well for me. Any help would be much appreciated.

Code:
sed -i '/"\<San Antonio Generators Running\>/i \\' outagereport.txt


Last edited by rbatte1; 06-26-2015 at 08:13 AM.. Reason: Removed FONT within the CODE tags
# 2  
Old 06-26-2015
Well, you are not searching the pattern that you cite in your text; there's an extra " and an extra s in the pattern. And, try \n for the replacement string.
This User Gave Thanks to RudiC For This Post:
# 3  
Old 06-26-2015
What output do you get?

I'm not a sed expert at all, but it would seem that you need to substitute your string with new-line and then your string. I think you are adding literally nothing with your code above.

Could you try something like this? (no promises at all):-
Code:
sed 's/"\<San Antonio Generators Running\>"/^J"\<San Antonio Generators Running\>"/' outagereport.txt

The ^J is a single character generated by pressing Ctrl+v then Ctrl+j

I hope that this helps, apologies if it does not.


Robin
This User Gave Thanks to rbatte1 For This Post:
# 4  
Old 06-26-2015
Traditional sed needs a multi-line string:
Code:
sed '/\<San Antonio Generators Running\>/ i\
hello world' outagereport.txt

Or
Code:
sed '
/\<San Antonio Generators Running\>/ i\
hello world
' outagereport.txt

GNU sed can continue on the same line:
Code:
sed '/\<San Antonio Generators Running\>/ ihello world' outagereport.txt

---------- Post updated at 11:11 AM ---------- Previous update was at 11:05 AM ----------

Just seeing that most GNU sed need the traditional approach when inserting a blank line:
Code:
sed '/\<San Antonio Generators Running\>/ i\
' outagereport.txt

This User Gave Thanks to MadeInGermany For This Post:
# 5  
Old 06-26-2015
You see, I said I didn't quite know what I was doing. Smilie

Thanks MadeInGermany for the corrections. Smilie


Robin
# 6  
Old 06-29-2015
Okay so I tried most of these and the following worked best for me:

Code:
sed '/\<San Antonio Generators Running\>/ i\\' outagereport.txt

This is what the output looked like prior to the SED command:
Code:
HOSTNAME USERNAME> cat outagereport.txt
San Antonio Generators Running
San Antonio Generator Alarms: Zero 
San Antonio Battery Discharge
San Antonio Battery Discharge Zero 
San Antonio Comm Power Fail
San Antonio Comm Power Fail Zero 
San Antonio Isolated Sites
San Antonio Isolated Sites Zero

This is the result with the SED command:
Code:
HOSTNAME USERNAME> sed '/\<San Antonio Generators Running\>/ i\\' outagereport.txt

San Antonio Generators Running
San Antonio Generator Alarms: Zero 
San Antonio Battery Discharge
San Antonio Battery Discharge Zero 
San Antonio Comm Power Fail
San Antonio Comm Power Fail Zero 
San Antonio Isolated Sites
San Antonio Isolated Sites Zero

Since some three of these strings have the same sequence, such as isolated sites and isolated sites Zero, how can I add a line above only the first instance?

Last edited by jbrass; 06-29-2015 at 12:48 AM.. Reason: used the wrong code tags, modified results
# 7  
Old 06-29-2015
sed does not have variables; in order to avoid the match in the further lines one must read them in a loop.
In traditional multi-line syntax (not possible in csh/tcsh):
Code:
sed '
/\<San Antonio Generators Running\>/ {
i\

:L
N
$!bL
}
' outagereport.txt

awk variables make things simpler:
Code:
awk '(!met && /\<San Antonio Generators Running\>/) {met=1; print ""} {print}' outagereport.txt

This User Gave Thanks to MadeInGermany For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed - How to insert line before the first blank line following a token

Hello. I have a config file (/etc/my_config_file) which may content : # # port for HTTP (descriptions, SOAP, media transfer) traffic port=8200 # network interfaces to serve, comma delimited network_interface=eth0 # set this to the directory you want scanned. # * if have multiple... (6 Replies)
Discussion started by: jcdole
6 Replies

2. Shell Programming and Scripting

To add a new line with specific text after the pattern is found using sed

hi guys, im trying to add the following line in my xml file <dbrollbacksegs <oa_var="s_db_rollback_segs">NOROLLBACK</dbrollbacksegs> when ever i find the following line <dbsharedpool oa_var="s_dbsharedpool_size">300000000</dbsharedpool> I have succedded till adding a new line... (1 Reply)
Discussion started by: smarlaku
1 Replies

3. Shell Programming and Scripting

sed to add text in new line

help i need to add a "nfsd" in new line after cron ex: cron rpcbind output: cron nfsd rpcbind i use sed -e "/cron/G; s/$/nfsd/" myfile output: cron nfsd rpcbindnfsd (5 Replies)
Discussion started by: jamilzain
5 Replies

4. Shell Programming and Scripting

sed add after line x new text from file

I've been playing with sed, trying to get it to insert the contents of somefile.txt after line 13 on anotherfile.txt. I tried searching for a line with regex and attempting to insert something on the next line with: find ./anotherfile.txt -type f -exec sed -i -e '/^dog/cat/' {} \; but it... (2 Replies)
Discussion started by: unclecameron
2 Replies

5. UNIX for Advanced & Expert Users

Sed - add text to start of line if 1st char anything but space

Problem: I have a lot of files, the files first line should always have 4 spaces before any text. Occasionally some of the files will miss the leading spaces and it's a problem. This is only in the first line. So if there are 4 spaces then text, do nothing. If there are not 4 spaces, add 4... (2 Replies)
Discussion started by: Vryali
2 Replies

6. Shell Programming and Scripting

Formatting a text file to get data in exact line by line

I have my data something like this SERIAL FIRSTOCCURRENCE NETPROTOCOL 1947430693 07/01/2009 05:16:40 FR SERIAL FIRSTOCCURRENCE NETPROTOCOL 1947430746 07/01/2009 05:18:05 FR I want the output as follows.... (1 Reply)
Discussion started by: rdhanek
1 Replies

7. UNIX for Dummies Questions & Answers

using sed or grep to find exact match of text

Hi, Can anyone help me with the text editing I need here. I have a file that contains the following lines for example: (line numbers are for illustration only) 1 Hello world fantasy. 2 Hello worldfuntastic. 3 Hello world wonderful. I would like to get all those lines of text that... (5 Replies)
Discussion started by: risk_sly
5 Replies

8. Shell Programming and Scripting

sed: delete regex line and next line if blank

Hi, I want to write a sed script which from batiato: batiato/giubbe: pip_b.2.txt pip_b.3.txt pip_b.3mmm.txt bennato: bennato/peterpan: 123.txt consoli: pip_a.12.txt daniele: (2 Replies)
Discussion started by: one71
2 Replies

9. Shell Programming and Scripting

insert text and add blank line

I need your help, I want to add a text every 2nd line and also a blank line after 3 line (In the output 2nd line is "changetype: modify" and every 4th line is blank line) Input file format dn: abc orclsourceobjectdn: abcd dn: bcd orclsourceobjectdn: bcda dn: cba orclsourceobjectdn:... (7 Replies)
Discussion started by: athidhi
7 Replies

10. Shell Programming and Scripting

sed.. Add new line after finding text

I know that this my be really simple, but I'm having a hard time accomplishing it. I am trying to add a new line of text after finding a particular string of text in a file. Here's what I'm getting: sed: command garbled: N/search_string/\\new_text/ I was using "N" to add a line after the... (3 Replies)
Discussion started by: douknownam
3 Replies
Login or Register to Ask a Question