The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



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

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-26-2007
rein rein is offline
Registered User
  
 

Join Date: Dec 2004
Location: Zürich
Posts: 146
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.
  #2 (permalink)  
Old 10-26-2007
Yogesh Sawant's Avatar
Yogesh Sawant Yogesh Sawant is offline Forum Staff  
Part Time Moderator and Full Time Dad
  
 

Join Date: Sep 2006
Location: Rossem, Tazenda
Posts: 1,086
This may not be the efficient solution:
Code:
#!/usr/bin/perl
# get_lines.pl
use strict;
my $check_next_line = "no";
my ($new_line, $mp);
while (<>) {
    chomp;
    if (/^NEW/) {
        $new_line = $_;  # save the line beginning with word NEW
        $check_next_line = "yes";
        $mp = (split (/\s+/, $_))[2];  # save the field containing MP code
        next;
    }
    elsif (/^CNL/) {
        if ($mp eq (split (/\s+/, $_))[2]) {
            print $new_line, "\n";
            print;
            print "\n";
        }
    }
    $check_next_line = "no";
}
Run this script as:
Code:
perl get_lines.pl new_file
  #3 (permalink)  
Old 10-26-2007
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,847
With awk:

Code:
awk '/^CNL/&&x=="NEW"$3&&$0=y RS$0
{x=$1$3;y=$0}' filename
Use nawk or /usr/xpg4/bin/awk on Solaris.
  #4 (permalink)  
Old 10-26-2007
rein rein is offline
Registered User
  
 

Join Date: Dec 2004
Location: Zürich
Posts: 146
Ok, great both solutions work.
  #5 (permalink)  
Old 10-26-2007
rein rein is offline
Registered User
  
 

Join Date: Dec 2004
Location: Zürich
Posts: 146
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]);
	}
	
}
anyone a solution in C perhaps? Just for fun?
  #6 (permalink)  
Old 10-26-2007
drl's Avatar
drl drl is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2007
Location: Saint Paul, MN USA / BSD, CentOS, Debian, OS X, Solaris
Posts: 704
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
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 06:30 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0