search pattern and read lines

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting search pattern and read lines
# 1  
Old 03-26-2012
search pattern and read lines


I have a huge de-limited file which has pattern :

99"9876"2010-11-21 12:51:01"J"MNOPQRS ID# 2-1234-1234-0099-9876-0         "" <<read>>
99"9876"2010-11-21 12:51:01"K"R-EMP# 01234567      (LOGOFF)               "" <<read>>
99"9876"2010-11-21 12:51:01"L"  *AUTO LOGOFF*                             "" <<read>>
99"9876"2010-11-21 12:51:01"M"END OF XXXXXXXXXXX #5180   END:12:51 PM     "" <<read>>
99"9876"2010-11-21 12:51:01"N"-2798    0        0        418      0       "" <<read>> 
99"1234"2010-11-21 12:12:12"J"MNOPQRS ID# 2-1234-1234-0099-1234-0         "" <<read>>
99"1234"2010-11-21 12:12:12"K"R-EMP# 01234567      (LOGIN)                "" <<read>>
99"1234"2010-11-21 12:12:12"L"  *AUTO LOGIN COMPLETED*                    "" <<do not read>>
99"1234"2010-11-21 12:12:12"M"END OF XXXXXXXXXXX #5180   END:12:12 PM     "" <<do not read>>
99"1234"2010-11-21 12:12:12"N"-2798    0        0        418      0       "" <<do not read>> 
99"5678"2010-11-21 12:10:01"J"MNOPQRS ID# 2-1234-1234-0099-9876-0         "" <<read>>
99"5678"2010-11-21 12:10:01"K"R-EMP# 01234567      (OPEN)                 "" <<read>>
99"5678"2010-11-21 12:10:01"L"  *AUTO LOGOFF*                             "" <<do not read>>
99"5678"2010-11-21 12:10:01"M"END OF XXXXXXXXXXX #5180   END:12:10 PM     "" <<do not read>>
99"5678"2010-11-21 12:10:01"N"-2798    0        0        418      0       "" <<do not read>>

I'm reading each line (while loop) and parsing the values to an output file based upon column-4.
When it reads 'K' type of line it extracts the value within the brackets in the last column(here, LOGOFF).

I need to achieve this:
If it is equal to LOGOFF or TURNOFF or CLOSE read the next lines as usual continously. If it is not equal to LOGOFF or TURNOFF or CLOSE, do not read the next lines that have the same combination of values present in column-1,2,3.
Here, do not read a line that has values
'column-1=99' && 'column-2=1234' && 'column-3=2010-11-21 12:12:12'

Using grep for searching the pattern and break for not reading the line is prefered.
# 2  
Old 03-26-2012
1. What do you want to do to the lines "read"?
2. Why would you not want to "read" the lines marked as 'do not read'?
# 3  
Old 03-26-2012
the text <<read>> and <<do not read>> are not part of the input file. That is why I highlighted with red.
The lines <read> are to be parsed to another output file.
<do not read> have to be skipped to be parsed to the output file
# 4  
Old 03-26-2012
Try this:

perl -F'"' -lane 'BEGIN { open O, "> outputfile.txt" }
if ($F[3] eq "K" && $F[4] =~ /LOGOFF|TURNOFF|CLOSE/) {
    $f = 0;
    print O;
elsif ($F[3] eq "K" && $F[4] !~ /LOGOFF|TURNOFF|CLOSE/) {
    $f = 1;
    print O;
    $a = $F[0]; $b = $F[1]; $c = $F[2];
elsif ($f == 1 && $F[0] == $a && $F[1] == $b && $F[2] eq $c) {
else {
    print O;
END { close O }' inputfile.txt

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search Pattern and Print lines in Single Column

Hi Experts I have small query where I request the into a single file Suppose: File1: {Unique entries} AA BB CC DD FileB: AA, 123 AA, 234 AA, 2345 CC, 123 CC, 5678 DD,123 BB, 7890 (5 Replies)
Discussion started by: navkanwal
5 Replies

2. Shell Programming and Scripting

Search pattern on logfile and search for day/dates and skip duplicate lines if any

Hi, I've written a script to search for an Oracle ORA- error on a log file, print that line and the .trc file associated with it as well as the dateline of when I assumed the error occured. In most it is the first dateline previous to the error. Unfortunately, this is not a fool proof script.... (2 Replies)
Discussion started by: newbie_01
2 Replies

3. Shell Programming and Scripting

sed search pattern and delete lines

Hello, i have a question. My problem is that i have a file like: TEST JOHN ADAM MICHAEL SEBASTIAN ANDY i want find for MICHAEL and want delete lines like this: TEST (4 Replies)
Discussion started by: eightball
4 Replies

4. Shell Programming and Scripting

How to search for pattern in odd lines?

Hi friends, I am looking for sed command/script that would search for a given fixed pattern on odd lines and then if it matches, prints the matching pattern and the next line. For example, in the example below, i am looking for pattern 0 and 1011 on odd lines. ########## start of example file... (10 Replies)
Discussion started by: kaaliakahn
10 Replies

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

6. Shell Programming and Scripting

read lines between search pattern

I have a file split something like 01/11/2010: No of users 100 02/11/2010: No of users 102 03/11/2010: No of users 99 ... I want to search the file for a particular date and then extract the following line with the date, something like 02/11/2010 No of users 102 I can grep... (6 Replies)
Discussion started by: gefa
6 Replies

7. Shell Programming and Scripting

Search for Pattern and Print including Lines in between

Gurus, I have a big file that needs to be sorted out and I cant figure out what to do. The file name is as below: Name: xxxx yyyy nnnn Description: dfffgs sdgsgsf hsfhhs afgghhjdgj fjklllll gsfhfh Updated: jafgadsgg gsg Corrected: date today The file consists of line like these. ... (13 Replies)
Discussion started by: The One
13 Replies

8. Shell Programming and Scripting

Pattern search in multiple lines

Hi, I have to search those statements from the file which starts from "shanky"(only shanky, shanky09 or 09shanky is not allowed) and ends with ");". These two string can be in a same line or different line. And also i have to negate those lines which starts with #. Can any one please give me... (2 Replies)
Discussion started by: shanky09
2 Replies

9. UNIX for Dummies Questions & Answers

Print lines between the search pattern

hi, I have a file say x.txt containing xxx 123 bla bla ... you xxx dfk dbf ... me xxx ... ... keeps on.. i need to search for pattern in the line starting xxx in the file. If pattern matched, I need to fetch all the lines till i find next xxx. (17 Replies)
Discussion started by: prsshini
17 Replies

10. Shell Programming and Scripting

Search file for pattern and grab some lines before pattern

I want to search a file for a string and then if the string is found I need the line that the string is on - but also the previous two lines from the file (that the pattern will not be found in) This is on solaris Can you help? (2 Replies)
Discussion started by: frustrated1
2 Replies
Login or Register to Ask a Question

Featured Tech Videos