Getting specific set of lines when pattern matches

grep print characters after match
awk print characters after match
grep rest of line after match
how to cut a string after a specific character in unix
grep lines after match until character
sed print lines after match
grep all lines after match
sed extract lines matching pattern

Input

abc  
cde  
efg  
xyz  
abc  
jkl  
mno  
xyz 

Now i want to get the lines between pattern abc and xyz but for the second match.

output

cde  
efg
awk '/abc/{count++;next} count==2{flag=1}  /xyz/ && flag{print val;val=flag=""} flag{val=val?val ORS $0:$0}' Input_file

Explanation:

awk '
/abc/{                    ##Checking condition here if a line is having string abc in it then do following.
   count++;               ##Increment variable named count with 1 each time cursor comes here.
   next}                  ##Using next keyword which will skip all further statements written.
count==2{                 ##Checking condition here if a variable count value is 2 here then do following.
   flag=1}                ##Setting variable named flag value is set to 1 here.
/xyz/ && flag{            ##Checking conditions here is a line is having string xyz and variable named flag is SET here then do following.
   print val;             ##Printing variable named val here.
   val=flag=""}           ##Nullifying variable val and flag here.
flag{                     ##Checking condition here if variable flag value is NOT NULL then do following.
   val=val?val ORS $0:$0  ##Create variable named val and concatenate its value with its own value each time cursor comes here.
}' Input_file             ##Mentioning Input_file name here.

Return only the portion of a line after a matching pattern, The canonical tool for that would be sed . sed -n -e 's/^.*stalled: //p'. Detailed explanation: -n means not to print anything by default. -e is followed by a sed  When you use sed you must do something for finding the second hit. First select the lines that match. That way you are sure that the first line will be abc when there are matches (I do not write ^abc$, perhaps a substring should match too). sed -n '/abc/,/xyz/ p' inputfile

Awk approach:

awk '/abc/ && ++c == 2{ f = 1; next }/xyz/{ f = 0 }f' file

The output:

jkl
mno

bash, This is very trivial with text processing utilities. For example, using sed : sed '1,/​pattern/!d' file. Meaning, match every line from the first one to the  You could do something like this. This would show you all the lines that have the pattern on it, You didn't specify how you wanted the output but at least that should show you the lines in the console. Select-String uses regex and dots have a special meaning in regex so the dots would have to be escaped with backslashes.

When you use sed you must do something for finding the second hit. First select the lines that match. That way you are sure that the first line will be abc when there are matches (I do not write ^abc$, perhaps a substring should match too).

sed -n '/abc/,/xyz/ p' inputfile 

Now delete until the second abc. When your input only has one abc all lines are deleted.

sed '/abc/,/abc/d'

When you alse delete lines from xyz until EOF, this will be

sed -n '/abc/,/xyz/ p' inputfile | sed '/abc/,/abc/d; /xyz/,$ d'

re – Regular Expressions, Finding Patterns in Text; Compiling Expressions; Multiple Matches; Pattern Syntax Regular expressions are text matching patterns described with a formal syntax. They are also an integral part of Unix command line utilities such as sed, grep, For a specific number of occurrences, use {m} after the pattern, where m is  I know that by using the "-A NUM" switch I can print specific number of trailing lines after each match. I am just wondering if it's possible to print trailing lines until a specific word is found after each match. e.g. When I search for "Word A" I want to see the line containing "Word A" and also the lines after it until the one containing

Count number of matches of a pattern | Vim Tips Wiki, Omit g to display the number of lines where the pattern matches: returns the cursor to where it started, then :%s///gn does the counting we want. version information about "gvi" and without knowing which specific command gives that error. Search in specific column for pattern and output entire line. p at the end makes sed print only lines matching the Pattern Search between specific lines and

Awk One-Liners Explained, Part III: Selective Printing and Deleting , String creation, array creation and update on selective printing of certain lines Awk has a special variable called "NR" that stands for "Number of Lines seen so statements" part get executed only on those lines that match "pattern" (pattern  Extract multiple matches into separate rows To extract multiple matches to separate cells, in separate rows, you can use an array formula based on INDEX and SMALL. In the example shown, the formula in E5 is: { = IFERROR ( INDEX ( names , SMALL ( IF ( groups = E$4 , ROW ( names ) - MIN ( ROW

grep, egrep, fgrep -- match patterns in a file, To match a character that is special to egrep, put a backslash (\) in front of the character. If grep finds a line that matches a pattern, it displays the entire line. does not display file names as prefixes when multiple input files are specified. -i length can greatly speed up the process of finding and displaying matching lines. Pattern matching adds new capabilities to those statements. In this article, you'll build a method that computes the area of different geometric shapes. But, you'll do it without resorting to object-oriented techniques and building a class hierarchy for the different shapes. You'll use pattern matching instead. As you go through this sample

Comments
  • Please read this: stackoverflow.com/help/someone-answers
  • Welcome to SO. Please read how to ask before post something.