Sponsored Content
Top Forums Shell Programming and Scripting How to identify exact text and then add a blank line above it using sed? Post 302948326 by jbrass on Sunday 28th of June 2015 11:45:58 PM
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
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
COMPACT(3)								 1								COMPACT(3)

compact - Create array containing variables and their values

SYNOPSIS
array compact (mixed $varname1, [mixed $...]) DESCRIPTION
Creates an array containing variables and their values. For each of these, compact(3) looks for a variable with that name in the current symbol table and adds it to the output array such that the variable name becomes the key and the contents of the variable become the value for that key. In short, it does the opposite of extract(3). Any strings that are not set will simply be skipped. PARAMETERS
o $varname1 -compact(3) takes a variable number of parameters. Each parameter can be either a string containing the name of the variable, or an array of variable names. The array can contain other arrays of variable names inside it; compact(3) handles it recursively. RETURN VALUES
Returns the output array with all the variables added to it. EXAMPLES
Example #1 compact(3) example <?php $city = "San Francisco"; $state = "CA"; $event = "SIGGRAPH"; $location_vars = array("city", "state"); $result = compact("event", "nothing_here", $location_vars); print_r($result); ?> The above example will output: Array ( [event] => SIGGRAPH [city] => San Francisco [state] => CA ) NOTES
Note Gotcha Because variable variables may not be used with PHP's Superglobal arrays within functions, the Superglobal arrays may not be passed into compact(3). SEE ALSO
extract(3). PHP Documentation Group COMPACT(3)
All times are GMT -4. The time now is 07:19 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy