sed for appending a line before a pattern and after another patter.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed for appending a line before a pattern and after another patter.
# 1  
Old 07-21-2017
sed for appending a line before a pattern and after another patter.

Hi All,

I'm appending a line before a pattern and after another pattern(Smilie but omitting the first pattern - same as if comes duplicates before the second pattern.

Also, I'm adding a word before the first pattern -

Here is my file
Code:
select blah blah
hello
select blah blah
;

select blah blah
hi
select blah blah
;

My expectation would be -

Code:
\o | grep org
anyword select blah blah
hello
select blah blah
;
\o

\o | grep org
anyword select blah blah
hi
select blah blah
;
\o

Here is my sed command I'm using with my file-

Code:
sed -e '/^ *[sS][eE][lL][eE][cC][tT]/ i \\\o | grep org' testfilepr.txt | sed -e 's/\(^ *[sS][eE][lL][eE][cC][tT]\)/anyword \1/g' | sed -e ' s/;/;\n\\\o/'

But my output comes out to be -

Code:
\o | grep org
anyword select blah blah
hello
\o | grep org
anyword select blah blah
;
\o

\o | grep org
anyword select blah blah
hi
\o | grep org
anyword select blah blah
;
\o

Please suggest.
# 2  
Old 07-21-2017
A sed multi-liner
Code:
sed '/^ *[sS][eE][lL][eE][cC][tT]/{
  i\
\\o grep org
  s/^ */anyword /
  :L
  $!N
  /;/!bL
  a\
\\o
}
'

The loop appends the following lines until the end marker ; so there are no further insertions.
This User Gave Thanks to MadeInGermany For This Post:
# 3  
Old 07-21-2017
Thanks MadeInGermany!! Your code is working for me in mentioned scenarios.

Could you please help out if we have some data like -

Code:
(
select blah blah
hello
select blah blah
;

select blah blah
hi
select blah blah
;

or
Code:
(select blah blah
hello
select blah blah
;

select blah blah
hi
select blah blah
;


In such cases, expectation would be -

Code:
\o | grep org
(
anyword select blah blah
hello
select blah blah
;
\o

\o | grep org
anyword select blah blah
hi
select blah blah
;
\o

or

Code:
\o | grep org
(anyword select blah blah
hello
select blah blah
;
\o

\o | grep org
anyword select blah blah
hi
select blah blah
;
\o

# 4  
Old 07-21-2017
This one tolerates one or more ( on two lines before the select
Code:
sed '
/^ *(/N
/^\( *(* *\n* *(* *\)\([sS][eE][lL][eE][cC][tT]\)/{
  s//\\o | grep org\
\1anyword \2/
  :L
  $!N
  /;/!bL
  a\
\\o
}
'

I am using the s (substitute) command for both insertions, because it can use the \( \) back references in the previous / /.
# 5  
Old 07-25-2017
Quote:
Originally Posted by MadeInGermany
This one tolerates one or more ( on two lines before the select
Code:
sed '
/^ *(/N
/^\( *(* *\n* *(* *\)\([sS][eE][lL][eE][cC][tT]\)/{
  s//\\o | grep org\
\1anyword \2/
  :L
  $!N
  /;/!bL
  a\
\\o
}
'

I am using the s (substitute) command for both insertions, because it can use the \( \) back references in the previous / /.

If I'm using below pattern, it will error out.

Code:
sed '
/^ *(/N
/^\( *(* *\n* *(* *\)\([sS][eE][lL][eE][cC][tT]\)/{
s//\\\o | grep \x27Send\\|Recv\\|Execute on\x27 | sed -e \x27s/^[ \\t]*//g\x27 -e \x27s/[|]*//g\x27  -e \x27s/[ \\t]*//g\x27 | cut -d\x27"\x27 -f2 | cut -d: -f1,2 | cut -d\\\\ -f1,2\
\1explain \2/
:L
$!N
/;/!bL
a\
\\o
}' filename.txt

# 6  
Old 07-26-2017
The sed uses s/search/replace/ i.e. / delimiters, so there may not be a / in the search and replace strings.
Use another delimiter, e.g. #:
Code:
sed '
/^ *(/N
/^\( *(* *\n* *(* *\)\([sS][eE][lL][eE][cC][tT]\)/{
  s##\\o | grep org\
\1anyword \2#
  :L
  $!N
  /;/!bL
  a\
\\o
}
' filename.txt

If required, this is also possible for the /search/, but the start delimiter needs to be escaped:
Code:
sed '
 \#^ *(#N
 \#^\( *(* *\n* *(* *\)\([sS][eE][lL][eE][cC][tT]\)#{
 ...

# 7  
Old 07-26-2017
This is a a "sed" thread, but for comparison sake, if there are always empty lines between pattern groups then an awk version could look like this:
Code:
awk '
  tolower($0)~/^\(?\n?select/ && $NF==";" {
    $0="\\o | grep org\n" $0 "\n\\o"
  }
  print
'  RS= ORS='\n\n' file

--

Last edited by Scrutinizer; 07-26-2017 at 12:51 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sed or Grep to delete line containing patter plus extra line

I'm new to using sed and grep commands, but have found them extremely useful. However I am having a hard time figuring this one out: Delete every line containing the word CEN and the next line as well. ie. test.txt blue 324 CEN green red blue 324 CEN green red blue to produce:... (2 Replies)
Discussion started by: rocketman88
2 Replies

2. Shell Programming and Scripting

awk;sed appending line to previous line....

I know this has been asked before but I just can't parse the syntax as explained. I have a set of files that has user information spread out over two lines that I wish to merge into one: User1NameLast User1NameFirst User1Address E-Mail:User1email User2NameLast User2NameFirst User2Address... (11 Replies)
Discussion started by: walkerwheeler
11 Replies

3. Shell Programming and Scripting

sed: appending alternate line after previous line

Hi all, I have to append every alternate line after its previous line. For example if my file has following contents line 1: unix is an OS line 2: it is open source line 3: it supports shell programming line 4: we can write shell scripts Required output should be line1: unix is an OS it is... (4 Replies)
Discussion started by: rish_max
4 Replies

4. UNIX for Dummies Questions & Answers

Grep for a pattern based on another patter

hi, I have looked at many grep threads and am unable to find something like this: please help. I have a file which is generated from a report generator and i am trying to load a whole lot of specific data into a table for the users. One field is causing me problems.All the rest i can manage.... (7 Replies)
Discussion started by: rock1
7 Replies

5. Shell Programming and Scripting

grep for a particular pattern and remove few lines above top and bottom of the patter

grep for a particular pattern and remove 5 lines above the pattern and 6 lines below the pattern root@server1 # cat filename Shell Programming and Scripting test1 Shell Programminsada asda dasd asd Shell Programming and Scripting Post New Thread Shell Programming and S sadsa ... (17 Replies)
Discussion started by: fed.linuxgossip
17 Replies

6. Shell Programming and Scripting

Sed - Appending a line with a variable on it

Hi, I searched the forum for this but couldn't find the answer. Basically I have a line of code I want to insert into a file using sed. The line of code is basically something like "address=1.1.1.1" where 1.1.1.1 is an IP Address that will vary depending on what the user enters. I'll just refer... (4 Replies)
Discussion started by: eltinator
4 Replies

7. UNIX for Dummies Questions & Answers

SED or AWK: Appending a line Identifier that changes with paragraph?

Have another question that has been eluding me all day. I have data file I'm trying to reformat so that each line is appended with an ID code, but the ID code needs to update as it searches through the file. I.e. ----Begin Original Datafile----- Condition = XXX Header Line 1 Header... (1 Reply)
Discussion started by: selkirk
1 Replies

8. Shell Programming and Scripting

appending with sed based on matched pattern

Hi, I want to know if you can input with sed but instead of specifing a line number like below I wan't to be able to insert based on a specific word or patttern. 10i\ Insert me after line 10 is this possible with sed or should I use AWK? Thanks Jack (2 Replies)
Discussion started by: jack1981
2 Replies

9. Shell Programming and Scripting

Appending line with sed works on Linux but not on Solaris

Hi folks, Our application installation uses "sed" command to append string after specific line or after line number. Both cases work perfect on Linux but fail on Solaris. The OS versions are Solaris 9 and Linux Red Hat AS 3. i.g: Linux: ----- file foo.txt aaa bbb ccc ddd root#... (4 Replies)
Discussion started by: nir_s
4 Replies

10. Shell Programming and Scripting

Appending line/lines with sed

Hi folks, I need to append line or bulk of lines into a file. For example,I have the following section in opmn.xml file: <process-type id="OC4J_RTEadmin_NIR" module-id="OC4J"> <module-data> <category id="start-parameters"> <data... (28 Replies)
Discussion started by: nir_s
28 Replies
Login or Register to Ask a Question