Get range out using sed or awk, only if given pattern match


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Get range out using sed or awk, only if given pattern match
# 1  
Old 09-23-2017
Get range out using sed or awk, only if given pattern match

Input:
Code:
START
OS:: UNIX
Release: xxx
Version: xxx
END
START
OS:: LINUX
Release: xxx
Version: xxx
END
START
OS:: Windows
Release: xxx
Version: xxx
END

Here i am trying to get all the information between START and END, only if i could match OS Type.

I can get all the data between the range START and END, however i dont know how to match the pattern

Using SED
Code:
sed -n '/START/,/END/p'

using AWK
Code:
awk '/START/,/END/'

Expected Output while searching for Linux:

Code:
START 
OS:: LINUX 
Release: xxx 
Version: xxx 
END

need help!!

Thanks in Advance


Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 09-23-2017 at 06:40 AM.. Reason: Added CODE tags.
# 2  
Old 09-23-2017
Hello Dharmaraja,

Welcome to forums, I hope you will enjoy learning/sharing knowledge here. Could you please try following and let me know if this helps you.
Code:
awk '/START/{count++} /OS.*LINUX/ && count{count++} /END/ && count==2{print val RS $0;val=count="";next}{val=val?val ORS $0:$0}/END/{count=val=""}'  Input_file

Output will be as follows.
Code:
START
OS:: LINUX
Release: xxx
Version: xxx
END

EDIT: Adding a non-one liner form of solution too now.
Code:
awk '
/START/{
  count++
}
/OS.*LINUX/ && count{
  count++
}
/END/ && count==2{
  print val RS $0;
  val=count="";
  next
}
{
  val=val?val ORS $0:$0
}
/END/{
  count=val=""
}
'   Input_file

Thanks,
R. Singh
# 3  
Old 09-23-2017
Try also
Code:
awk -vRS="END\n" -vORS="END\n" '/[Ll][Ii][Nn][Uu][Xx]/' file
START
OS:: LINUX
Release: xxx
Version: xxx
END

# 4  
Old 09-23-2017
You can try this one too
Code:
sed -n '/START/{N;/LINUX/{:A;N;/END/!bA;p;q}}' infile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

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: 1 2 3 4 5 B4Srt1--Variable-0000 B4Srt2--Variable-1111 Srt 6 7 8 9 10 End (3 Replies)
Discussion started by: y2jacky
3 Replies

2. Shell Programming and Scripting

sed one Liner inverse range match-help required

cat test.txt a c d e g (2 Replies)
Discussion started by: TomG
2 Replies

3. Shell Programming and Scripting

Pattern match with awk/sed - help

I need to grep for the pattern text inside the square brackets which are in red and not in green..my current code greps patterns both of them, which i don't want Input fileref|XP_002371341.1| oxoacyl-ACP reductase, putative gb|EPT24759.1| 3-ketoacyl-(acyl-carrier-protein) reductase ... (2 Replies)
Discussion started by: selvankj
2 Replies

4. Shell Programming and Scripting

Sorting content between match pattern and move on with awk and sed

S 0.0 0.0 (reg, inst050) k e f d c S 0.0 0.0 (mux, m030) k g r s x v S 0.0 0.0 (reg, inst020) q s n m (12 Replies)
Discussion started by: ctphua
12 Replies

5. Shell Programming and Scripting

sed : match one pattern then the next consecutive second pattern not working

Ive used this snippet of code on a solaris box thousands of times. But it isnt working on the new linux box sed -n '/interface LoopBack0/{N;/ ip address /p;}' *.conf its driving me nuts !! Is there something Im missing ? (7 Replies)
Discussion started by: popeye
7 Replies

6. Shell Programming and Scripting

Awk-sed help : to remove first and last line with pattern match:

awk , sed Experts, I want to remove first and last line after pattern match "vg" : I am trying : # sed '1d;$d' works fine , but where the last line is not having vg entry it is deleting one line of data. - So it should check for the pattern vg if present , then it should delete the line ,... (5 Replies)
Discussion started by: rveri
5 Replies

7. Shell Programming and Scripting

match range of different numbers by AWK

if the column1 and 2 in both files has same key (for example "a" and "a1") compare each first key value(a1 of a) of input2 (for example 1-4 or 65-69 not 70-100 or 44-40 etc) with all the values in input1. if the range of first key value in input2 is outof range in input1 values named it as out... (54 Replies)
Discussion started by: repinementer
54 Replies

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

9. Shell Programming and Scripting

SED - adding blank line after each Range Match

the following range matching works great but i wish to add a blank line after each range result set... which i've tried and researched to no avail MY INPUT DATA: CURRENT CODE I'M USING: sed -n '/*$/,/;/p' $INPUT_FILE RESULTS I'M GETTING: RESULT I looking to... (5 Replies)
Discussion started by: danmauer
5 Replies

10. 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
Login or Register to Ask a Question