The UNIX and Linux Forums  


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
Search files with specfic extention and later search content lovi_v UNIX for Advanced & Expert Users 2 07-14-2008 01:05 PM
Perl: Search for string on line then search and replace text Crypto Shell Programming and Scripting 4 01-04-2008 10:24 AM
Can I search columns and print lines? Ant1815 UNIX for Dummies Questions & Answers 2 04-26-2007 08:01 AM
Need help in AWK;Search String and rearrange columns spring_buck Shell Programming and Scripting 2 04-05-2007 12:40 PM
Advanced Search Problems.. Search by User Name Neo Post Here to Contact Site Administrators and Moderators 1 05-19-2003 01:28 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 09-16-2008
akil akil is offline
Registered User
  
 

Join Date: Jun 2008
Posts: 50
search for columns

Hi
I need to search for two columns START_DT,END_DT in file,if exists give tha table name.Please help


Code:
ex:

FILE.TXT
CREATE TABLE AB
(CD1 VARCHAR2(100),
CD2 VARCHAR(50),
START_DT DATE,
END_DT DATE);

CREATE TABLE AB1
(CD1 VARCHAR2(100),
CD2 VARCHAR(50));

o/p should be 
Tablename :AB


Thanks,
Akil
  #2 (permalink)  
Old 09-16-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652

Code:
awk '/CREATE TABLE /{table=$2; start=0; }
/START_DT/ { start++ }
/END_DT/ && start  { print "Table name: " table }' file

... assuming END_DT always comes after START_DT, and CREATE TABLE marks the beginning of a new table.

Basic Unix design philosophy dictates that a tool like this should simply just display the table names, without any user-friendly mark-up.
  #3 (permalink)  
Old 09-16-2008
akil akil is offline
Registered User
  
 

Join Date: Jun 2008
Posts: 50
Hi Era,
Its working ,Thanks for your reply .

Thanks,
Akil
  #4 (permalink)  
Old 09-16-2008
akil akil is offline
Registered User
  
 

Join Date: Jun 2008
Posts: 50
Hi Era,

I want to check for case senstive

example: create table...
Start_dt..
End_dt

Thanks,
Akil
  #5 (permalink)  
Old 09-17-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
If your awk has the toupper function, you can use that to do case-insensitive comparisons.


Code:
awk 'toupper($0) ~ /CREATE TABLE /{table=$2; start=0; }
toupper($0) ~ /START_DT/ { start++ }
toupper($0) ~ /END_DT/ && start  { print table }' file

This creates an uppercase version of the input line, and compares that against the regular expressions. (I suppose it would be more economical to only invoke toupper once. If this is a time-critical operation, we can look at that.)
  #6 (permalink)  
Old 09-17-2008
summer_cherry summer_cherry is offline Forum Advisor  
Registered User
  
 

Join Date: Jun 2007
Location: Beijing China
Posts: 1,092

Code:
undef $/;
open FH,"<file.txt";
while(<FH>){
        @arr=split("CREATE",$_);
        for($i=0;$i<=$#arr;$i++){
                $arr[$i]=~tr/\n/ /;
		@temp=split(" ",$arr[$i]);
		if($temp[6] eq "START_DT" && $temp[8] eq "END_DT"){
			print "Tablename:",$temp[1],"\n";
		}
	}
}
close(FH);

  #7 (permalink)  
Old 09-17-2008
akil akil is offline
Registered User
  
 

Join Date: Jun 2008
Posts: 50
Hi Era,
Thanks your great help .This waht I am looking ,Thanks a lot.


Code:
Perl script which posted by summer_cherry its not working ,
temp[6] && $temp[8] {5th and 7th fields) 
If Date columns may be different sequence
in some of the tables,In this case how can the below script can work?

ex:

Create table ..
cd1  char(10),
start_dt date,
desc varcha(200),
end_dt date));

##

undef $/;
open FH,"<file.txt";
while(<FH>){
        @arr=split("CREATE",$_);
        for($i=0;$i<=$#arr;$i++){
                $arr[$i]=~tr/\n/ /;
		@temp=split(" ",$arr[$i]);
		if($temp[6] eq "START_DT" && $temp[8] eq "END_DT"){
			print "Tablename:",$temp[1],"\n";
		}
	}
}
close(FH);
###

Thanks,
Akil

Last edited by akil; 09-17-2008 at 11:20 AM..
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 12:13 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