Reporting lines above a particular pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Reporting lines above a particular pattern
# 8  
Old 06-25-2012
Try this...

Code:
$ awk '/Startpoint/{for(i=1;i<5;i++){line=line"\n"$0;getline}}/Path.*good/{a=1}/Path.*bad/{line=""} /VIOL/&& a{a=0;print line"\n"$0;line="";}' input.txt

---------- Post updated at 07:32 PM ---------- Previous update was at 07:28 PM ----------

Quote:
Originally Posted by krsnadasa
Hi Itkamraj,

I still need to understanding this logic, if you donot mind.

Code:
 
awk '/Path.*good/{a=1}!a{line=line"\n"$0}/VIOL/{if(a){print line"\n"$0}}' input.txt

Code:
/Path.*good/{a=1}  -- if line matches this pattern (Path.*good), then set the variable a=1

!a {line=line"\n"$0} --- line variable will not get append when it already have the value as 1

/VIOL/ -- if line matches this pattern (VIOL), then check whether it is good or not ( by checking the if(a) ) and print the line

# 9  
Old 06-25-2012
how can i put the else clause to this command ...
ie :when path is good do this...
else for any path (other than good)....ignore this
ie it may not be bad always...
it can be moderate,bad,typical......etc
Quote:
Originally Posted by itkamaraj
Try this...

Code:
$ awk '/Startpoint/{for(i=1;i<5;i++){line=line"\n"$0;getline}}/Path.*good/{a=1}/Path.*bad/{line=""} /VIOL/&& a{a=0;print line"\n"$0;line="";}' input.txt

---------- Post updated at 07:32 PM ---------- Previous update was at 07:28 PM ----------



Code:
/Path.*good/{a=1}  -- if line matches this pattern (Path.*good), then set the variable a=1

!a {line=line"\n"$0} --- line variable will not get append when it already have the value as 1

/VIOL/ -- if line matches this pattern (VIOL), then check whether it is good or not ( by checking the if(a) ) and print the line


Last edited by dll_fpga; 06-25-2012 at 11:26 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

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

2. Shell Programming and Scripting

How to delete all lines before a particular pattern when the pattern is defined in a variable?

I have a file Line 1 a Line 22 Line 33 Line 1 b Line 22 Line 1 c Line 4 Line 5 I want to delete all lines before last occurrence of a line which contains something which is defined in a variable. Say a variable var contains 'Line 1', then I need the following in the output. ... (21 Replies)
Discussion started by: Soham
21 Replies

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

4. UNIX for Dummies Questions & Answers

Grep -v lines starting with pattern 1 and not matching pattern 2

Hi all! Thanks for taking the time to view this! I want to grep out all lines of a file that starts with pattern 1 but also does not match with the second pattern. Example: Drink a soda Eat a banana Eat multiple bananas Drink an apple juice Eat an apple Eat multiple apples I... (8 Replies)
Discussion started by: demmel
8 Replies

5. Shell Programming and Scripting

Match Pattern and print pattern and multiple lines into one line

Hello Experts , require help . See below output: File inputs ------------------------------------------ Server Host = mike id rl images allocated last updated density vimages expiration last read <------- STATUS ------->... (4 Replies)
Discussion started by: tigerhills
4 Replies

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

7. Shell Programming and Scripting

Need one liner to search pattern and print everything expect 6 lines from where pattern match made

i need to search for a pattern from a big file and print everything expect the next 6 lines from where the pattern match was made. (8 Replies)
Discussion started by: chidori
8 Replies

8. UNIX for Dummies Questions & Answers

Matching and reporting near-similar lines in a file

Hi, I have a file with the lines as below: C_10_A05_T7 C_10_A06_SP6 C_10_B05_SP6 C_10_B05_T7 C_10_B01_SP6 C_10_B01_T7 C_12_G07_SP6 C_12_G11_SP6 C_12_G11_T7 C_2_H18_T7 C_2_I02_SP6 C_2_I02_T7 C_2_I13_SP6 C_2_I17_SP6 The four segments of each line are connected by '_' symbols. I... (7 Replies)
Discussion started by: Fahmida
7 Replies

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

10. Shell Programming and Scripting

counting the lines matching a pattern, in between two pattern, and generate a tab

Hi all, I'm looking for some help. I have a file (very long) that is organized like below: >Cluster 0 0 283nt, >01_FRYJ6ZM12HMXZS... at +/99% 1 279nt, >01_FRYJ6ZM12HN12A... at +/99% 2 281nt, >01_FRYJ6ZM12HM4TS... at +/99% 3 283nt, >01_FRYJ6ZM12HM946... at +/99% 4 279nt,... (4 Replies)
Discussion started by: d.chauliac
4 Replies
Login or Register to Ask a Question