look for two consecutive lines in all text files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting look for two consecutive lines in all text files
# 1  
Old 09-02-2010
look for two consecutive lines in all text files

How to get (a list of) all the text files in the current directory and subdirectories which has the following two consecutive lines:
ctrl_end_date=2009
ctrl_process=EXPIRED
OR
ctrl_end_date=2010
ctrl_process=EXPIRED

i.e.
(ctrl_end_date=2009 OR ctrl_end_date=2010)
AND
ctrl_process=EXPIRED

---------- Post updated at 11:07 AM ---------- Previous update was at 10:41 AM ----------

a related question:
How to get (a list of) all the text files in the current directory and subdirectories which has the following two consecutive lines:
ctrl_end_date=nnnnnnnn
ctrl_process=EXPIRED

where nnnnnnnn is <= 20100902
e.g.
nnnnnnnn is a date string
20070614 and 20100503 meet the criteria
but 20101102 does not.
# 2  
Old 09-02-2010
For your first question you can use
Code:
awk -F\= '{if(x&&$0=="ctrl_process=EXPIRED"){a[FILENAME]++}}{x=($NF>2008&&$NF<2011&&$1=="ctrl_end_date")?1:0}END{for(i in a){print i}}' *



---------- Post updated at 12:25 PM ---------- Previous update was at 12:17 PM ----------

This is for your second question.
Code:
awk -F\= '{if(x&&$0=="ctrl_process=EXPIRED"){a[FILENAME]++}}{x=($NF<=20100903&&$1=="ctrl_end_date")?1:0}END{for(i in a){print i}}' *

not a final solution but you can start from here Smilie
Please use [code] tags when you post code or data sample

Last edited by danmero; 09-02-2010 at 01:19 PM.. Reason: Fix test
# 3  
Old 09-02-2010
Code:
grep -R -e 'ctrl_end_date=2009' -e 'ctrl_end_date=2010' --after-context=1 | grep -e 'ctrl_process=EXPIRED'|awk {print $1}


Last edited by Scott; 09-02-2010 at 02:41 PM.. Reason: Code tags, please...
# 4  
Old 09-02-2010
Please help to fix the following error.
Thanks.
Code:
$ awk -F\= '{if(x&&$0=="ctrl_process=EXPIRED"){a[FILENAME]++}}{x=($NF>2008&&$NF<2011&&$1=="ctrl_end
_date")?1:0}END{for(i in a){print i}}' *

awk: cmd. line:1: fatal: cannot open file `testing' for reading (Is a directory)
# 5  
Old 09-02-2010
Quote:
Originally Posted by albertkao
Please help to fix the following error.
Thanks.
Code:
$ awk -F\= '{if(x&&$0=="ctrl_process=EXPIRED"){a[FILENAME]++}}{x=($NF>2008&&$NF<2011&&$1=="ctrl_end
_date")?1:0}END{for(i in a){print i}}' *

awk: cmd. line:1: fatal: cannot open file `testing' for reading (Is a directory)
Everithing should be in one line
Code:
awk -F\= '{if(x&&$0=="ctrl_process=EXPIRED"){a[FILENAME]++}}{x=($NF>2008&&$NF<2011&&$1=="ctrl_end_date")?1:0}END{for(i in a){print i}}' *

# 6  
Old 09-03-2010
Code:
awk -F = '/^ctrl_end_date/&& $NF<=20100902 {print;getline;print;next}' infile

# 7  
Old 09-03-2010
Quote:
Originally Posted by rdcwayx
Code:
awk -F = '/^ctrl_end_date/&& $NF<=20100902 {print;getline;print;next}' infile

What if the next line is not
Code:
ctrl_process=EXPIRED

Anyway the OP is looking for the filename(s).
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Delete all CONSECUTIVE text lines from file shell scripting

Hi I have a text file like below. THe content of the text will vary. Entire text file have four consecutive lines followed with blank line. I want to delete the occurrence of the two consicutive lines in the text file. I don't have pattern to match and delete. Just i need to delete all... (5 Replies)
Discussion started by: RJSKR28
5 Replies

2. Shell Programming and Scripting

Grep three consecutive lines if each lines contains certain string

say we have : 2914 | REQUEST | whatever 2914 | RESPONSE | whatever 2914 | SUCCESS | whatever 2985 | RESPONSE | whatever 2986 | REQUEST | whatever 2990 | REQUEST | whatever 2985 | RESPONSE | whatever 2996 | REQUEST | whatever 2010 | SUCCESS | whatever 2013 | REQUEST | whatever 2013 |... (7 Replies)
Discussion started by: Saumitra Pandey
7 Replies

3. UNIX for Dummies Questions & Answers

Finding the same pattern in three consecutive lines in several files in a directory

I know how to search for a pattern/regular expression in many files that I have in a directory. For example, by doing this: grep -Ril "News/U.S." . I can find which files contain the pattern "News/U.S." in a directory. I am unable to accomplish about how to extend this code so that it can... (1 Reply)
Discussion started by: shoaibjameel123
1 Replies

4. Shell Programming and Scripting

Read n lines from a text files getting n from within the text file

I dont even have a sample script cause I dont know where to start from. My data lookes like this > sat#16 #data: 15 site:UNZA baseline: 205.9151 0.008 -165.2465 35.8109 40.6685 21.9148 121.1446 26.4629 -18.4976 33.8722 0.017 -165.2243 48.2201 40.6908 ... (8 Replies)
Discussion started by: malandisa
8 Replies

5. Shell Programming and Scripting

Grep couple of consecutive lines if each lines contains certain string

Hello, I want to extract from a file like : 20120530025502914 | REQUEST | whatever 20120530025502968 | RESPONSE | whatever 20120530025502985 | RESPONSE | whatever 20120530025502996 | REQUEST | whatever 20120530025503013 | REQUEST | whatever 20120530025503045 | RESPONSE | whatever I want... (14 Replies)
Discussion started by: black_fender
14 Replies

6. Shell Programming and Scripting

Merge two non-consecutive lines

Hello - First post here. I need help combining two lines that are non-consecutive in a file. Using sed, awk or perl preferably. So the file looks as follows. Please note, the "Line#:" is there only for reference. The lines can only be distinguished by whether they have "start" or "done" in... (2 Replies)
Discussion started by: munkee
2 Replies

7. UNIX for Dummies Questions & Answers

want to merge two consecutive lines.

Hi All, I want to merge two consecutive lines. Currently the output is :--> crmplp1 cmis461 No Online cmis462 No Offline crmplp2 cmis462 No Online cmis463 No ... (6 Replies)
Discussion started by: pank29
6 Replies

8. UNIX Desktop Questions & Answers

How to concatenate consecutive lines

I have a few lines like -- feature 1, subfeat 0, type 3, subtype 1, value 0, -- feature 1, subfeat 0, type 1, subtype 1, value 0, I would like to concatenate the... (1 Reply)
Discussion started by: shivi707
1 Replies

9. Shell Programming and Scripting

How to delete first 5 lines and last five lines in all text files

Hi I want to delete first five and last five lines in text files without opening the file and also i want to keep the same file name for all the files. Thanks in advance!!! Ragav (10 Replies)
Discussion started by: ragavendran31
10 Replies

10. Shell Programming and Scripting

Appending Consecutive lines

Hi, I have a file containing a single field on every row. What I need is to append one on to the end of another, e.g. The input file looks like this: nnnnn mmmmmm nnnnn mmmmmm I need it to look like this: nnnnn mmmmmm nnnnn mmmmmm Any ideas would be much appreciated,... (8 Replies)
Discussion started by: pondlife
8 Replies
Login or Register to Ask a Question