How to limit the search to 'n' occurrences within a line


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users How to limit the search to 'n' occurrences within a line
# 1  
Old 08-31-2009
Question How to limit the search to 'n' occurrences within a line

Hello All,

I am trying to search one pattern across a file and then print it. But i need to delimit the search per line to 2 occurrences. How do i do that?

Regards.
# 2  
Old 08-31-2009
Try...
Code:
 
awk -v var="searchstring" '{str=$0;cnt=gsub(var,arr);if(cnt==2)print str}' infile

# 3  
Old 08-31-2009
Code:
perl -nle '{print $_ if scalar(@_ = $_ =~ /pattern/g) == 2}' infile

# 4  
Old 09-03-2009
Question Not working for me :-(

Hello malcomex999, Vi-Curious,

Thanks for the replies.I tried both the suggestions but none of them worked for me :-(. My file contains -

~/Desktop$ cat b.txt
hello hello hello hello
good morning
good evening

I am trying to search for hello string in this file and am expecting it to print 'hello' only twice in the output. Please let me know if i am doing something wrong there.

~/Desktop$ awk -v var="hello" '{str=$0;cnt=gsub(var,arr);if(cnt==2)print str}' b.txt
<No OutPut here>

~/Desktop$ perl -nle '{print $_ if scalar(@_ == $_ =~ /hello/g) == 2}' b.txt
<No OutPut here>

Regards.



# 5  
Old 09-03-2009
A slight modification in the Vi-Curious does the trick.

Code:
perl -nle '{print "$_[0] $_[1]" if (@_ = $_ =~ /hello/g) }' b.txt

# 6  
Old 09-03-2009
Or awk...

Code:
 
awk -v var="hello" '{cnt=gsub(var,arr);if(cnt>=2)print var,var}' infile

# 7  
Old 09-03-2009
Quote:
Originally Posted by Linuxee
Hello malcomex999, Vi-Curious,

~/Desktop$ perl -nle '{print $_ if scalar(@_ == $_ =~ /hello/g) == 2}' b.txt
<No OutPut here>
The reason there was no output was because the first occurrence of == in your example should have been just a single =. Others have already posted follow-up examples to your latest query so I'll just say that this would have printed the entire line if it contained exactly 2 occurrences of the search pattern. It wasn't clear from your initial post that you wanted to print out the first 2 occurrences of (only) the search pattern if it occurred 2 or more times in the line.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Delete multiple occurrences of the same pattern on a line but the first

The lines that I am trying to format look like Device ID: j01-01, IP address: 10.10.10.36, IP address: 10.10.10.35, IP address: 10.10.102.201, Platform: 8040, Capabilities: Host , Interface: GigabitEthernet9/45, Port ID (outgoing port): e0k,Here is what I have so far but it... (4 Replies)
Discussion started by: dis0wned
4 Replies

2. UNIX for Advanced & Expert Users

sed REGEX to print multiple occurrences of a pattern from a line

I have a line that I need to parse through and extract a pattern that occurs multiple times in it. Example line: getInfoCall: info received please proceed, getInfoCall: info received please proceed, getInfoCall: info received please proceed, getInfoCall: info received please proceed,... (4 Replies)
Discussion started by: Vidhyaprakash
4 Replies

3. Shell Programming and Scripting

How to search number of occurrences of a particular string in a file through vi editor?

i have one file, i am doing 'vi Filename' now i want to search for particular string and i want to know how many times that string occurs in whole file (5 Replies)
Discussion started by: sheelsadan
5 Replies

4. Programming

Limit line for perl

Hey guys, can help me out with this? How do i limit output for xml to 50 character? i tried *below* but doesnt work, it still print more than 50 characters. Thanks in advance printf "%-50s", "$testline\n"; (4 Replies)
Discussion started by: Nick1097
4 Replies

5. Shell Programming and Scripting

grep by limit search

Hi I am in new in unix,can any one tell how to grep the data by limit. suppose I have below data:- is :mSecs is :mSecs is :mSecs is :mSecs requirement is how to grep the data which is having count greater than 1000 msecs only. thanks in adnavce. (2 Replies)
Discussion started by: abhigrkist
2 Replies

6. Shell Programming and Scripting

sed: how to limit pattern search to first instance only

I need to reduce a file's size below 50MB by deleting chucks of text. The following sed does this. sed '/^begpattern/,/endpattern/d' myfile However, it's possible that the file size can get below 50MB by just deleting the first instance of the pattern. How do I code that into sed? Or can awk... (8 Replies)
Discussion started by: mariod1049
8 Replies

7. Shell Programming and Scripting

perl search and replace - search in first line and replance in 2nd line

Dear All, i want to search particular string and want to replance next line value. following is the test file. search string is tmp,??? ,10:1 "???" may contain any 3 character it should remain the same and next line replace with ,10:50 tmp,123 --- if match tmp,??? then... (3 Replies)
Discussion started by: arvindng
3 Replies

8. Shell Programming and Scripting

sed replace multiple occurrences on the same line, but not all

Hi there! I am really enjoying working with sed. I am trying to come up with a sed command to replace some occurrences (not all) in the same line, for instance: I have a command which the output will be: 200.300.400.5 0A 0B 0C 01 02 03 being that the last 6 strings are actually one... (7 Replies)
Discussion started by: ppucci
7 Replies

9. UNIX for Dummies Questions & Answers

Search and Count Occurrences of Pattern in a File

I need to search and count the occurrences of a pattern in a file. The catch here is it's a pattern and not a word ( not necessarily delimited by spaces). For eg. if ABCD is the pattern I need to search and count, it can come in all flavors like (ABCD, ABCD), XYZ.ABCD=100, XYZ.ABCD>=500,... (6 Replies)
Discussion started by: tektips
6 Replies

10. Shell Programming and Scripting

grep line length limit

Hi Friends, I am having a funny problem with grep. When I run grep 'expr' file.txt things work fine. But when try to get the line number using the -n option, i.e, grep -n 'expr' file.txt I get a message, "grep: 0652-226 Maximum line length of 2048 exceeded." If the line has more than... (3 Replies)
Discussion started by: hnhegde
3 Replies
Login or Register to Ask a Question