Sponsored Content
Top Forums UNIX for Advanced & Expert Users How to limit the search to 'n' occurrences within a line Post 302350164 by Linuxee on Thursday 3rd of September 2009 02:53:45 AM
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.



 

10 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

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

10. 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
STREAM_FILTER_REGISTER(3)						 1						 STREAM_FILTER_REGISTER(3)

stream_filter_register - Register a user defined stream filter

SYNOPSIS
bool stream_filter_register (string $filtername, string $classname) DESCRIPTION
stream_filter_register(3) allows you to implement your own filter on any registered stream used with all the other filesystem functions (such as fopen(3), fread(3) etc.). PARAMETERS
o $filtername - The filter name to be registered. o $classname - To implement a filter, you need to define a class as an extension of php_user_filter with a number of member functions. When performing read/write operations on the stream to which your filter is attached, PHP will pass the data through your filter (and any other filters attached to that stream) so that the data may be modified as desired. You must implement the methods exactly as described in php_user_filter - doing otherwise will lead to undefined behaviour. RETURN VALUES
Returns TRUE on success or FALSE on failure. stream_filter_register(3) will return FALSE if the $filtername is already defined. EXAMPLES
Example #1 Filter for capitalizing characters on foo-bar.txt stream The example below implements a filter named strtoupper on the foo-bar.txt stream which will capitalize all letter characters writ- ten to/read from that stream. <?php /* Define our filter class */ class strtoupper_filter extends php_user_filter { function filter($in, $out, &$consumed, $closing) { while ($bucket = stream_bucket_make_writeable($in)) { $bucket->data = strtoupper($bucket->data); $consumed += $bucket->datalen; stream_bucket_append($out, $bucket); } return PSFS_PASS_ON; } } /* Register our filter with PHP */ stream_filter_register("strtoupper", "strtoupper_filter") or die("Failed to register filter"); $fp = fopen("foo-bar.txt", "w"); /* Attach the registered filter to the stream just opened */ stream_filter_append($fp, "strtoupper"); fwrite($fp, "Line1 "); fwrite($fp, "Word - 2 "); fwrite($fp, "Easy As 123 "); fclose($fp); /* Read the contents back out */ readfile("foo-bar.txt"); ?> The above example will output: LINE1 WORD - 2 EASY AS 123 Example #2 Registering a generic filter class to match multiple filter names. <?php /* Define our filter class */ class string_filter extends php_user_filter { var $mode; function filter($in, $out, &$consumed, $closing) { while ($bucket = stream_bucket_make_writeable($in)) { if ($this->mode == 1) { $bucket->data = strtoupper($bucket->data); } elseif ($this->mode == 0) { $bucket->data = strtolower($bucket->data); } $consumed += $bucket->datalen; stream_bucket_append($out, $bucket); } return PSFS_PASS_ON; } function onCreate() { if ($this->filtername == 'str.toupper') { $this->mode = 1; } elseif ($this->filtername == 'str.tolower') { $this->mode = 0; } else { /* Some other str.* filter was asked for, report failure so that PHP will keep looking */ return false; } return true; } } /* Register our filter with PHP */ stream_filter_register("str.*", "string_filter") or die("Failed to register filter"); $fp = fopen("foo-bar.txt", "w"); /* Attach the registered filter to the stream just opened We could alternately bind to str.tolower here */ stream_filter_append($fp, "str.toupper"); fwrite($fp, "Line1 "); fwrite($fp, "Word - 2 "); fwrite($fp, "Easy As 123 "); fclose($fp); /* Read the contents back out */ readfile("foo-bar.txt"); ?> The above example will output: LINE1 WORD - 2 EASY AS 123 SEE ALSO
stream_wrapper_register(3), stream_filter_append(3), stream_filter_prepend(3). PHP Documentation Group STREAM_FILTER_REGISTER(3)
All times are GMT -4. The time now is 01:24 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy