![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Pattern Matching problem in UNIX | maxmave | Shell Programming and Scripting | 2 | 06-03-2008 01:19 AM |
| pattern matching problem | namishtiwari | Shell Programming and Scripting | 2 | 05-23-2008 07:33 AM |
| problem with CASE pattern matching | gummysweets | Shell Programming and Scripting | 2 | 03-18-2008 11:30 AM |
| pattern matching in an if-then | lumix | Shell Programming and Scripting | 4 | 12-14-2007 04:25 PM |
| Pattern Matching | danhodges99 | UNIX for Dummies Questions & Answers | 2 | 02-27-2003 03:03 PM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
pattern matching problem
I have a file with the following contents;
NEW 85174 MP081 /29OCT07 CNL 85986 MP098 /28OCT07 NEW 86014 MP098 /28OCT07 NEW 86051 MP097 /27OCT07 CNL 86084 MP097 /27OCT07 Now I have to retrieve all lines that start with NEW and where the next line starts with CNL and where the MP codes are the same in both lines. So it has to return the last two lines from this example. |
|
||||
|
With awk it took me 3 hours and no result and with Java 30 mins and result ... the awk solution is much smaller though but I can't understand it.
Code:
import java.io.*;
public class GetLine {
private String prevWord = "";
private String prevLine = "";
private void parse(String fileName){
try {
BufferedReader in = new BufferedReader(new FileReader(fileName));
System.out.println("Reading file: " +fileName);
String line = "";
while ((line = in.readLine()) != null) {
//System.out.println(str);
String [] word = line.split("[\t]");
//System.out.println(word[2]);
if ( prevWord.equals(word[2])){
System.out.println(prevLine);
System.out.println(line);
}
prevWord = word[2];
prevLine = line;
}
in.close();
} catch (IOException e) {
System.err.println(e);
}
}
public static void main(String [] args){
GetLine getLine = new GetLine();
getLine.parse(args[0]);
}
}
|
|
|||||
|
Hi, rein.
You mentioned the awk and Java. It looked like the perl script from Yogesh Sawant would work -- did you time it? This looks mostly IO bound, so except for coding the algorithm, I would not expect drastically different times. For example, my experience is that perl is very close to c for IO cases, but not so close for arithmetic-dense code. The perl might be made a bit more efficient by using the suffix "o" for matching constant patterns, and possibly not splitting more fields than needed -- but I'd think those are making very small contributions ... cheers, drl |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|