how to display only the pattern


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users how to display only the pattern
# 1  
Old 12-30-2003
how to display only the pattern

Hi,
I have a file with 500 Lines and I want to search for a pattern within this file which starts with sm_ and ends with ). However I just want to print the pattern only and not the entire line. How do I do it ?

Thanks,
p
# 2  
Old 12-30-2003
Hi,

GNU grep has the -o option that does what you want, it will only print the pattern, not the entire line...

from the man page:
-o, --only-matching Show only the part of a matching line that matches PATTERN.
# 3  
Old 12-30-2003
Not tested...
Code:
awk 'match($0,"sm_.*)"){print substr($0,RSTART,RLENGTH)}' $FILE

# 4  
Old 01-05-2004
Using sed...

sed 's/^.*\(PATTERN\).*$/\1/' yourFile
# 5  
Old 01-05-2004
Quote:
sed 's/^.*\(PATTERN\).*$/\1/' yourFile
does not work, since it will print all 500 lines of the file, not just those lines which contain the pattern. A possible solution using sed would be...
Code:
sed -n '/sm_.*)/{s/^.*\(sm_.*)\).*$/\1/;p;}' $FILE

# 6  
Old 01-05-2004
I must be half asleep today ;-)

This is what you want...

sed -n 's/^.*\(PATTERN\).*$/\1/p' yourFile

or even better (in terms of performance)...

sed -n '/PATTERN/s/^.*\(PATTERN\).*$/\1/p' yourFile

So for the OPs specific example...

sed -n '/sm_.*)/s/^.*\(sm_.*)\).*$/\1/p' yourFile

(which is pretty much what you have Ygor)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk command to display particular pattern

Hi I am using awk to print 10,11 column but it is not displaying required output. Please let me know how I can browse through the line and extract the required one Example: I have below 2 lines in file seq 49960| Thu Apr 19 10:57:40.726182 2018|Len: 89|GAP for CL/U18 9P-NC (CL90U8)) gap... (10 Replies)
Discussion started by: sdosanjh
10 Replies

2. Shell Programming and Scripting

How to display when nth line match a pattern?

Hi All, I have sample of listing as following Database 2 entry: Database alias = PXRES Database name = PXRES Local database directory = /db2/data1/db2phnx Database release level = d.00 Comment ... (3 Replies)
Discussion started by: ckwan
3 Replies

3. Shell Programming and Scripting

Grep pattern and display all lines below

Hi I need to grep for a patter and display all lines below the pattern. For ex: say my file has the below lines file1 file2 file3 file4 file5 I NEED to grep for patter file3 and display all lines below the pattern. do we have an option to get this data. Let me know if you require... (5 Replies)
Discussion started by: venkidhadha
5 Replies

4. Shell Programming and Scripting

Display 2 lines before and after a particular pattern

Hi team, Is it possible to display 2 lines after a particular pattern in a shell script. For example in a file which has the below contents. Mummy Daddy Son Daughter Children Aunty Uncle Grandma Grandpa Son Father Mother Brother-in-law I want to display 2 lines before and after... (1 Reply)
Discussion started by: balamv
1 Replies

5. Shell Programming and Scripting

Display Row to Columns Pattern

Hi All, I have following pattern of output Number of executions = 1 Number of compilations = 1 Total execution time (sec.microsec)= 0.263898 Statement text = ALTER TABLE DSSSTG.SG2_MIB_MIBP04 ACTIVATE NOT LOGGED INITIALLY WITH EMPTY... (4 Replies)
Discussion started by: ckwan
4 Replies

6. UNIX for Dummies Questions & Answers

Display blocks containing specific pattern

Hi, I have a file containing multiple entries. Each block starts with <BEGIN and ends with <END. Sample data is given below <BEGIN IMSI=095001202630; MSISDN=00145132916; DEFCALL=TS11; CURRENTNAM=BOTH; CAT=COMMON; TBS=TS11&TS12&TS21&TS22; CARDTYPE=SIM; ... (2 Replies)
Discussion started by: krabu
2 Replies

7. Shell Programming and Scripting

Want to grep for a pattern and display the content above that pattern

Hi, When we have a failure, sometimes we just step restart the job from the next step. Later when we open the log for analysis of the failure, it is becoming difficult to go to the failure part. For eg., if it is a 1000 line log, the failure may be at 500th line. so wat i want to do is, grep... (6 Replies)
Discussion started by: ajayakunuri
6 Replies

8. Shell Programming and Scripting

display content between all similar tags pattern

hi, I m stuck at a point for more than 3days. My doubt is pretty simple.. I have a web page content in $content. ( got this by using LWP).. Now I want to display the content matching a pattern. I tried if($content =~ m{<div class="abc">(.*?)</div>}s){ print $1;} that will... (4 Replies)
Discussion started by: therockravi
4 Replies

9. Shell Programming and Scripting

To find the files with pattern and display

Hi all, Im looking for a script(bash,perl) to find the files which has content with ORA (Mostly will be from log file)if we find those content,need to send a mail alert to the team members with the files which are matched. This script should run daily so that it should search for the files... (1 Reply)
Discussion started by: preetha_83
1 Replies

10. UNIX for Dummies Questions & Answers

Display few rows before and after the pattern is found

hi, i'm trying to write a script in ksh that checks the build log for errors. I use egrep to serch for a pattern list. When the pattern is found i want to print 2 rows before and 2 rows after the line where the patern is found, but i don't know how to do it. (3 Replies)
Discussion started by: lavinia_f
3 Replies
Login or Register to Ask a Question