Search a String and display only word.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search a String and display only word.
# 1  
Old 08-06-2010
Question Search a String and display only word.

Hello Gurus,

Apologies if this Q has been repeated but i was not able to find it Smilie

I have an input file:
-------------------------------
Replace DB.Employee
as
select
column1
column2
from DB_T.Emp
and DB.Test
and
DB.Dept
and
DB_T.Ter;

------------------------

I want whole word that matches "DB." or "DB_T." i.e my output file should look like:

---------------------------
DB.Employee
DB_T.Emp
DB.Test
DB.Dept
DB_T.Ter
---------------------------
Grep is displaying the whole line but i just want the word.
Thank you in advance.
# 2  
Old 08-07-2010
Hi

Code:
# grep -o 'DB.*[^ ;]'  file
DB.Employee
DB_T.Emp
DB.Test
DB.Dept
DB_T.Ter
#

If -o option not available in your grep, try this:
Code:
# grep 'DB' file | sed  's/.*\(DB.*[^ ;]\);*/\1/'
DB.Employee
DB_T.Emp
DB.Test
DB.Dept
DB_T.Ter
#

Guru.

Last edited by guruprasadpr; 08-07-2010 at 01:48 AM..
# 3  
Old 08-07-2010
Your small set of input implied that there'd be only one DB.* per record. If you have some records which have multiple DB.* words you want to list, then you'll need something like this:

Code:
# fomatted for easier reading; comments completely unnecessary....
# cannonicalise each word in <> branch to keep if matched
# if not matched; delete record; loop to next record
# delete all chrs upto first <
# delete all characters between > <  (multiple matches on same record)
# delete leading <
# delete trailing > and any trailing whitespace
# replace remaining > with newline (separate multiple to one per output record)

sed 's/DB[._][^ \t]*/<&>/g; t keep
d 
: keep
{
        s/[^<]*//;
        s/> [^<]*/> /g;
        s/<//g;
        s/>[ \t]*$//;
        s/> */\n/g; 
}' <file

This will print each DB.* word on a separate line. This example assumes whitespace as seperators; you can add any punctuation that should act as a delimiter to the appropriate class specifications. It also will not match something like DBxxNotMe.

Code:
Input:
Replace DB.Employee  DBxNOTme
as 
select 
column1
column2
from DB_T.Emp bar DB_T.foo DB.goo
and DB_Testx
DB.Testy      and more text
DB.Testz and    more text and DB.Testa
and 
DB.Depta
and
DB_T.Ter;

Output:                          
DB.Employee
DB_T.Emp
DB_T.foo
DB.goo
DB_Testx
DB.Testy
DB.Testz
DB.Testa
DB.Depta
DB_T.Ter;

# 4  
Old 08-08-2010
Code:
tr -s "[ \t]" "\n" <file | grep 'DB[._].*'

# 5  
Old 08-09-2010
Thanks All..!!!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search string in multiple files and display column wise

I have 3 files. Each of those files have the same number of records, however certain records have different values. I would like to grep the field in ALL 3 files and display the output with only the differences in column wise and if possible line number File1 Name = Joe Age = 33... (3 Replies)
Discussion started by: sidnow
3 Replies

2. UNIX for Beginners Questions & Answers

Search a string and display its location on the entire string and make a text file

I want to search a small string in a large string and find the locations of the string. For this I used grep "string" -ob <file name where the large string is stored>. Now this gives me the locations of that string. Now how do I store these locations in a text file. Please use CODE tags as... (7 Replies)
Discussion started by: ANKIT ROY
7 Replies

3. Shell Programming and Scripting

Search and replace the string with new word using xml tags

Hi All i need to replace the url1 inside <remote> tag in below xml in first instance and in the second instance with url2. any help appreciated <locations> <hudson.scm.SubversionSCM_-ModuleLocation> <remote>https://svn2015.com/svn/repos/internalshard</remote> ... (4 Replies)
Discussion started by: madankumar.t@hp
4 Replies

4. Shell Programming and Scripting

Grep word after last occurance of string and display next few lines

Hi, I wanted to grep string "ERROR" and "WORNING" after last occurrence of String "Starting" only and wanted to display two lines after searched ERROR and WORNING string and one line before. I have following cronjob log file "errorlog" file and I have written the code for same in Unix as below... (17 Replies)
Discussion started by: nes
17 Replies

5. Shell Programming and Scripting

Search string in unix and print whole matching word

Hi I have requirement to search string starting with specific characters and print whole matching word in that string. example mystr="ATTRIBUTE NAME="Event Name" VALUE="Execute"" I want to search by passing "NAME=" and result should be NAME="Event Name". i am using below command but... (3 Replies)
Discussion started by: tmalik79
3 Replies

6. Shell Programming and Scripting

search-word-print-specific-string

Hi, Our input xml looks like: <doc> <str name="account_id">1111</str> <str name="prd_id">DHEP155EK</str> </doc> - <doc> <str name="account_id">6666</str> <str name="prd_id">394531662</str> </doc> - <doc> <str name="account_id">6666</str> <str... (1 Reply)
Discussion started by: Jassz
1 Replies

7. Shell Programming and Scripting

search a word and print specific string using awk

Hi, I have list of directory paths in a variable and i want to delete those dirs and if dir does not exist then search that string and get the correct path from xml file after that delete the correct directory. i tried to use grep and it prints the entire line from the search.once i get the entire... (7 Replies)
Discussion started by: dragon.1431
7 Replies

8. UNIX for Advanced & Expert Users

Parsing String, Search then display rows

Get occurence of "open" considering duplicates(get the last open). Once you are pointing to the last open count 2 rows to get the correct data. Every begin and end statement, there is a "close" and "open". There can be many "close" and "open" within the begin and end statement but we are... (9 Replies)
Discussion started by: buddyme
9 Replies

9. Shell Programming and Scripting

Search for string and display those NOT found

In my script I read a input file and search all the files in a directory and it's sub-directories for that string using: find . -type f -print | xargs grep $var1 This just displays all the lines the string was found on. Too much data. What I need is to store in a file one time those... (17 Replies)
Discussion started by: John Rihn
17 Replies

10. Shell Programming and Scripting

How to check a string conating a search word or not ...

Hi fiends, I need some help. i have a var called fname="req_detail_TAM.dat" and is have a search string var as tmp="TAM". Now my requirement is i need to check whether a "fname" is contains "search str" or not. if search string is part of the filename, then i need to store value 1 in a... (2 Replies)
Discussion started by: manu.vmr
2 Replies
Login or Register to Ask a Question