Sponsored Content
Top Forums Shell Programming and Scripting sed Range Pattern and 2 lines before Start Pattern Post 302960204 by y2jacky on Thursday 12th of November 2015 04:11:29 AM
Old 11-12-2015
sed Range Pattern and 2 lines before Start Pattern

Hi all,

I have been searching all over Google but I am unable to find a solution for a particular result that I am trying to achieve.

Consider the following input:

Code:
1
2
3
4
5
B4Srt1--Variable-0000
B4Srt2--Variable-1111
Srt
6
7
8
9
10
End
11
12
13
B4Srt1-Variable-12361
B4Srt2-Variable-68931
Srt
14
15
End


I would like to extract 2 lines (these 2 lines can be different) above "Srt" (fixed) till "End"(fixed).

Desired output:

Code:
B4Srt1--Variable-0000
B4Srt2--Variable-1111
Srt
6
7
8
9
10
End
B4Srt1-Variable-12361
B4Srt2-Variable-68931
Srt
14
15
End

I understand that using the sed range pattern can help me on this.

Code:
sed -n '/Srt/,/End/' filename

However, the challenge that I am facing is that only "Srt" is fixed while the two lines above "Srt" always change, therefore I am unable to use them as a pattern. How do I modify the sed command such that I can include the two lines above "Srt", somewhat like grep -B2?

Please help on this.

Last edited by Don Cragun; 11-12-2015 at 05:25 AM.. Reason: Add CODE and ICODE tags.
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help needed in sed range pattern

Hi all, I am using sed for extracting the lines that occurs between the 2 patterns using the following command: sed -n '/pattern1/,/pattern2/' filename The above command has no problem and works fine. But I was wondering if there is a way to quit sed when it has extracted the range at... (3 Replies)
Discussion started by: sank
3 Replies

2. Shell Programming and Scripting

sed: Find start of pattern and extract text to end of line, including the pattern

This is my first post, please be nice. I have tried to google and read different tutorials. The task at hand is: Input file input.txt (example) abc123defhij-E-1234jslo 456ujs-W-abXjklp From this file the task is to grep the -E- and -W- strings that are unique and write a new file... (5 Replies)
Discussion started by: TestTomas
5 Replies

3. Shell Programming and Scripting

sed pattern range

Hi guys, trying to replace a '#' with a ' ' (space) but only between the brackets '(' and ')' N="text1#text2#text3(var1#var2#var3)" N=`echo $N |sed '/(/,/) s/#. //'` echo $N Looking for an output of "text1#text2#text3(var1 var2 var3)" Any ideas? (15 Replies)
Discussion started by: mikepegg
15 Replies

4. Shell Programming and Scripting

How to combine lines within range of pattern

I've a file say having line 1 line 2 (NP line 3 line 4 line 5) line 6 I want to combine lines starting from (NP and ending with ) then it will look like line 1 line 2 (NP line3 line4 line5) line 6 I tried using sed '/(NP/,/)$/ s/\n/ /' but it's not working. Any help please? ... (8 Replies)
Discussion started by: neg
8 Replies

5. Shell Programming and Scripting

How to print range of lines using sed when pattern has special character "["

Hi, My input has much more lines, but few of them are below pin(IDF) { direction : input; drc_pinsigtype : signal; pin(SELDIV6) { direction : input; drc_pinsigtype : ... (3 Replies)
Discussion started by: nehashine
3 Replies

6. Shell Programming and Scripting

sed pattern to delete lines containing a pattern, except the first occurance

Hello sed gurus. I am using ksh on Sun and have a file created by concatenating several other files. All files contain header rows. I just need to keep the first occurrence and remove all other header rows. header for file 1111 2222 3333 header for file 1111 2222 3333 header for file... (8 Replies)
Discussion started by: gary_w
8 Replies

7. Shell Programming and Scripting

Sed print range of lines between line number and pattern

Hi, I have a file as below This is the line one This is the line two <\XMLTAG> This is the line three This is the line four <\XMLTAG> Output of the SED command need to be as below. This is the line one This is the line two <\XMLTAG> Please do the need to needful to... (4 Replies)
Discussion started by: RMN
4 Replies

8. Shell Programming and Scripting

Sed: printing lines AFTER pattern matching EXCLUDING the line containing the pattern

'Hi I'm using the following code to extract the lines(and redirect them to a txt file) after the pattern match. But the output is inclusive of the line with pattern match. Which option is to be used to exclude the line containing the pattern? sed -n '/Conn.*User/,$p' > consumers.txt (11 Replies)
Discussion started by: essem
11 Replies

9. Shell Programming and Scripting

sed -- Find pattern -- print remainder -- plus lines up to pattern -- Minus pattern

The intended result should be : PDF converters 'empty line' gpdftext and pdftotext?xml version="1.0"?> xml:space="preserve"><note-content version="0.1" xmlns:/tomboy/link" xmlns:size="http://beatniksoftware.com/tomboy/size">PDF converters gpdftext and pdftotext</note-content>... (9 Replies)
Discussion started by: Klasform
9 Replies

10. Shell Programming and Scripting

[sed] Finding and sticking the pattern to the beginning of successive lines up to the next pattern

I have a file like below. 2018.07.01, Sunday 09:27 some text 123456789 0 21 0.06 0.07 0.00 2018.07.02, Monday 09:31 some text 123456789 1 41 0.26 0.32 0.00 09:39 some text 456789012 1 0.07 0.09 0.09 09:45 some text 932469494 1 55 0.29 0.36 0.00 16:49 some text 123456789 0 48 0.12 0.15 0.00... (9 Replies)
Discussion started by: father_7
9 Replies
RDF::Trine::Node::Variable(3pm) 			User Contributed Perl Documentation			   RDF::Trine::Node::Variable(3pm)

NAME
RDF::Trine::Node::Variable - RDF Node class for variables VERSION
This document describes RDF::Trine::Node::Variable version 1.000 METHODS
Beyond the methods documented below, this class inherits methods from the RDF::Trine::Node class. "new ( $name )" Returns a new Variable structure. "name" Returns the name of the variable. "sse" Returns the SSE string for this variable. "as_string" Returns a string representation of the node. "value" Returns the variable name. "as_ntriples" Returns the node in a string form suitable for NTriples serialization. "type" Returns the type string of this node. "equal ( $node )" Returns true if the two nodes are equal, false otherwise. BUGS
Please report any bugs or feature requests to through the GitHub web interface at <https://github.com/kasei/perlrdf/issues>. AUTHOR
Gregory Todd Williams "<gwilliams@cpan.org>" COPYRIGHT
Copyright (c) 2006-2012 Gregory Todd Williams. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. perl v5.14.2 2012-06-29 RDF::Trine::Node::Variable(3pm)
All times are GMT -4. The time now is 02:33 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy