search a word from file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting search a word from file
# 1  
Old 02-08-2008
search a word from file

Hello,
I have a file which contains some SQL statements. I need to take out the table name from the file. Table name will always end with "_t". can anyone help me in getting that?
for e.g.
---
SQL_STMT do_sql_insert: cmd="insert into account_t ( poid_DB, poid_ID0, poid_TYPE, poid_REV, business_type, account_type, name, account_no,
SQL_STMT dm_search_robj: cmd="select poid_DB, poid_ID0, poid_TYPE, poid_REV from bal_grp_t where bal_grp_t.account_obj_ID0 = :1
SQL_STMT do_sql_update: cmd="update account_t set poid_rev = poid_rev + 1, mod_t = :mod_t, currency=:currency, account_no=:account_no
---
Thanks in advance.

Last edited by vishy; 02-08-2008 at 12:39 PM..
# 2  
Old 02-08-2008
Something like ...
Code:
grep -v "_t" FILENAME

... obviously this will also match ...
Code:
UPDATE table set col="welcome to _town" ... ;

... you should / could use your favorite diff to inspect your changes ...
Code:
grep -v "_t" FILENAME >FILENAME2
diff FILENAME FILENAME2

# 3  
Old 02-08-2008
this will give me the lines which does not contain "_t". what i want is the table name ....
so from my example i shld get...
account_t
bal_grp_t
account_t
# 4  
Old 02-08-2008
Code:
#!/usr/bin/perl
#
# cat SQLFILENAME | perl CODEFROMBELOWFILE.pl
#

use strict;

my %words;

while(my $line = <STDIN>)
{
   # munge line here to just deal with (select|delete) .* from (.*) (where|order by|group by|having) issues

    for my $word (split(/\s+/, $line))
    {
        $words{$word}++ if($word =~ m/^\w+_t$/i);
    }
}

for my $k (sort keys %words)
{
    print "$k\n";
}


Last edited by HPAVC; 02-08-2008 at 01:46 PM.. Reason: added usage
# 5  
Old 02-08-2008
well you could try something like:

Code:
sed 's/.* \(.*_t \).*/\1/g' infile

or

Code:
tr " " "\n" <infile | grep _t$

but since you have things like mod_t in there, I'm guessing without a bit better parsing it won't be quite as exact as you'd like - may be good enough though depending on your overall sample size / whether this is a one-off etc
# 6  
Old 02-08-2008
Quote:
Originally Posted by HPAVC
Code:
#!/usr/bin/perl

use strict;

my %words;

while(my $line = <STDIN>)
{
    for my $word (split(/\s+/, $line))
    {
        $words{$word}++ if($word =~ m/^\w+_t$/i);
    }
}

for my $k (sort keys %words)
{
    print "$k\n";
}

Thanks for the solution...
but i m new to shell scripting... i m not able to understand this code.
can u pl. explain where i have to pass my file name and how to execute this code?
# 7  
Old 02-08-2008
save the code as a file e.g. perl-script

chmod the file to be exacutable.

then the following will work:

Code:
#  cat infile | ./perl-script
account_t
bal_grp_t
mod_t

FYI - this gives the same o/p as the sed cmd I suggested above...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search for a word in file and output to another

Hello Experts! I am trying to perform a common task, searching data from within a file and placing what is found into another.However, I have not been able to figure the “How to” with this situation. I need to search through all lines within this text and pull the first positional attributes... (5 Replies)
Discussion started by: leepet
5 Replies

2. Shell Programming and Scripting

Search for a specific word and print only the word from the input file

Hi, I have a sample file as shown below, I am looking for sed or any command which prints the complete word only from the input file. Ex: $ cat "sample.log" I am searching for a word which is present in this file We can do a pattern search using grep but I need to cut only the word which... (1 Reply)
Discussion started by: mohan_kumarcs
1 Replies

3. Shell Programming and Scripting

How to search a word in a file?

How to search a particular word in a file. in my file contains lacks of rows. (2 Replies)
Discussion started by: pmreddy
2 Replies

4. Shell Programming and Scripting

search a word and copy the file

Hi need help with a script or command My requirement is - I need to do a "ls -ltr tcserver*.syslog" files in /tmp, direct the output to a file ls -ltr tcserv*.syslog | grep "Jan 31" | awk '{printf "\n" $9}' > jandat.logs -Then open each file in above list and search for string/word,... (4 Replies)
Discussion started by: karghum
4 Replies

5. Shell Programming and Scripting

How to search for a word in a particular column of a file

How to search for a word like "computer" in a column (eg: 4th field) of a '***' delimited file and add a column at the end of the record which denotes 'Y' if present and 'N' if not. After this, we need to again check for words like 'Dell' but not 'DellXPS' in 5th field and again add another column... (5 Replies)
Discussion started by: Jassz
5 Replies

6. Shell Programming and Scripting

Search for last instance of word in a file

Hi I'm trying to search for the last instance of the word 'cache' in a HTML file that I have downloaded from YouTube. I'm using the following syntax, but an error is thrown when I try it. grep -f "cache" Also I wish to append the above grep command to the below so that the search for cache... (3 Replies)
Discussion started by: colmbell
3 Replies

7. UNIX for Dummies Questions & Answers

Search a specific word from any one of the file.

how do i Search a specific word from any one of the file.? (1 Reply)
Discussion started by: ritusubash
1 Replies

8. Shell Programming and Scripting

Search for word in a xml file and replace it with something else

Hello Unix Users, I am very new to Unix so I am not sure how do I do the following. I need a script such that when I type the following in the command prompt > . scriptName.sh wordToBeReplaced DirectoryLocation will find the word someword located in a somefile.xml in DirectoryLocation... (8 Replies)
Discussion started by: 5211171
8 Replies

9. Shell Programming and Scripting

To search a file for a specific word in a file using shell script

Hi All, I have a sql output file has below. I want to get the values 200000040 and 1055.49 .Can anyone help me to write a shell script to get this. ACCOUNT_NO ------------------------------------------------------------ BILL_NO ... (8 Replies)
Discussion started by: girish.raos
8 Replies

10. UNIX for Dummies Questions & Answers

search file for word, then assign to variable

Hi all, Trying to search a file for one word only, then assign that word to a variable. Not sure if this is a grep or awk (or either) function. Should be a simple operation. Example: This file contains the string "COMPLETE". I would like to pull that word out and assign it to a... (2 Replies)
Discussion started by: dejit
2 Replies
Login or Register to Ask a Question