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
# 8  
Old 09-03-2009
Bug

Hello All,

I am not familiar with the perl and awk syntax :-(.

Thanks a lot! It worked as expected !

Regards.
# 9  
Old 09-03-2009
Quote:
Originally Posted by Linuxee
I am not familiar with the perl and awk syntax :-(.
try google


The example I posted:
Code:
 perl -nle '{print $_ if scalar(@_ = $_ =~ /pattern/g) == 2}' infile

Code:
perl -nle 'expression' infile

loop over the file 'infile' and execute 'expression' on each line

Code:
$_ =~ /pattern/g

Bind to the current line ($_) and search for all occurences of pattern on that line. By default, this operation will work on $_ so that could have been omitted and this part could have been specified as /pattern/g.

Code:
@_ = $_ =~ /pattern/g

In list context, m//g, or simply //g, returns all of the matched occurrences of pattern. So @_ will be a list containing however many occurrences of the pattern existed.

Code:
scalar(@_ = ... )

Take the list @_ and tell me how many items it contains.

Code:
if scalar(...) == 2

Check to see if it contains 2 items (occurrences of pattern).

Code:
print $_ if scalar(...) == 2

Print the current line from the file if it contains exactly 2 occurrences of the search pattern.... which is what I thought you wanted.
# 10  
Old 09-04-2009
Thanks Vi-Curious,

Yes. Will google and learn more :-)

Thanks for the detailed explanation!

Regards.
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