11-13-2009
Quote:
Originally Posted by
paragkalra
I have few of questions related to [GNU] Grep given below:
1. Like Perl, is it possible in Grep to negate characters in square brackets.
Yes, and this is true of probably all regular expressions. The syntax is nearly identical across all platforms; however there may be variations on handling special characters such as the dash (-), the brackets ([ and ]) and the caret (^) itself. Character classes, such as [[:digit:]] work too, but there are slight differences.
Quote:
Originally Posted by
paragkalra
2. How do we match special characters in Grep?
You escape them with the backslash (\), as you usually do in perl. The problem is that in the normal grep mode, some special characters
[ ] ^ $ . * are always on, and some
( ) ? + { } are on when you escape them. However, if you use "egrep" (or -E with GNU grep), you get the situation where all special characters are "magical"
unless you escape them.
Quote:
Originally Posted by
paragkalra
3. My aim is to find only those files starting with dn, and containing the string 'ou=' exactly 3 times. Can anybody why this is not working:
Yeah, because grep treats each separate line as a new search space. So the whole idea of matching a set of 3 lines with
(atom){3} won't work, period. So grep is not the right tool here
unless you first use tr or sed to translate all newlines into some other caracter (x1B for instance). Then you need to use egrep or grep -E and your regexp *might* work.
10 More Discussions You Might Find Interesting
1. UNIX for Dummies Questions & Answers
Hi all,
My boss wants me to find out how often e-m users are accessing their account:confused:. The mail server keeps log of all logins. I want to use grep the 'usernames', but it should come out the moment it first encounters the username in the log. Can I do that? I want to avoid 10+ greps... (2 Replies)
Discussion started by: nitin
2 Replies
2. UNIX for Dummies Questions & Answers
Hello every one, I have read a little about SED and GREP but I do not know how to do this:
Using SED or GREP:
"reverse all three letter words"
"replace the last two digits in any string of digits by zeros (0)"
"remove lines that start and end with the same word"
and I have more like... (5 Replies)
Discussion started by: Lem2003
5 Replies
3. UNIX for Dummies Questions & Answers
I have the data file:
A
1
2
3
BBB
4
5
6
A
7
8
9
I want to grep "A" then-skip a line-then-add two sublines:
I my command:
grep +3 "A" datafile (8 Replies)
Discussion started by: bobo
8 Replies
4. UNIX for Dummies Questions & Answers
Hi,
Is it possible to display a specific number of lines starting from a line having a particular text using grep command?
e.g. I have a text file with the contents below:
AAA
BBB
CCC
DDD
EEE
FFF
I want to display 3 lines starting with the line having "BBB" to get the result below:... (11 Replies)
Discussion started by: stevefox
11 Replies
5. UNIX for Dummies Questions & Answers
Hello All,
1.)
I am searching for ".exe" in a text file
2.)
I need to search for a hexadecimal entree of at least four digits (8 Replies)
Discussion started by: Omega1589
8 Replies
6. Shell Programming and Scripting
Hi guys. I need to filter some values from a number of log files. One of the files is:
Interconnect Utilisation Results:
Achieved Maximum Number of Concurrent Connections: 17
Statistics for Average Number of Concurrent Connections:
Point Estimation: Confidence Interval: ... (2 Replies)
Discussion started by: Faaz0
2 Replies
7. UNIX for Dummies Questions & Answers
This was mistaken as homework in a different forum, but is not. These are questions that are close to what I am trying to do at work.
QUESTION1:
How do you grep only an exact string. I am using Solaris10 and do not have any GNU products installed.
Contents of car.txt
CAR1_KEY0
CAR1_KEY1... (1 Reply)
Discussion started by: thibodc
1 Replies
8. UNIX for Dummies Questions & Answers
I have different things that I was trying to do but am kind of struggling with this since I'm a Linux noob. The backround is that I have two files with student names in the same directory, and each file lists the student name, their major and their grade level. What is the most efficient way to... (6 Replies)
Discussion started by: tastybeer
6 Replies
9. Shell Programming and Scripting
Hi. I have a txt file. I need to make a copy of the lines which are beginning with a mobile phone number, or a fix phone number. I have to copy thoose lines in numbers.txt, after that i have to delete then from the originally file. In numbers.txt i need to write a prefix before each number. if the... (1 Reply)
Discussion started by: T720
1 Replies
10. Homework & Coursework Questions
1. The problem statement, all variables and given/known data:
So i'll probably get told off for this but I have a few problems and rather than clog up the whole forum I'll post them here. Please bare in mind I am a complete novice when it comes to all this and so if you help please treat me like a... (4 Replies)
Discussion started by: jamesb18
4 Replies
LEARN ABOUT DEBIAN
poe::filter::stackable
POE::Filter::Stackable(3pm) User Contributed Perl Documentation POE::Filter::Stackable(3pm)
NAME
POE::Filter::Stackable - combine multiple POE::Filter objects
SYNOPSIS
#!perl
use POE qw(
Wheel::FollowTail
Filter::Line Filter::Grep Filter::Stackable
);
POE::Session->create(
inline_states => {
_start => sub {
my $parse_input_as_lines = POE::Filter::Line->new();
my $select_sudo_log_lines = POE::Filter::Grep->new(
Put => sub { 1 },
Get => sub {
my $input = shift;
return $input =~ /sudo[d+]/i;
},
);
my $filter_stack = POE::Filter::Stackable->new(
Filters => [
$parse_input_as_lines, # first on get, last on put
$select_sudo_log_lines, # first on put, last on get
]
);
$_[HEAP]{tailor} = POE::Wheel::FollowTail->new(
Filename => "/var/log/system.log",
InputEvent => "got_log_line",
Filter => $filter_stack,
);
},
got_log_line => sub {
print "Log: $_[ARG0]
";
}
}
);
POE::Kernel->run();
exit;
DESCRIPTION
POE::Filter::Stackable combines multiple filters together in such a way that they appear to be a single filter. All the usual POE::Filter
methods work, but data is secretly passed through the stacked filters before it is returned. POE::Wheel objects and stand-alone programs
need no modifications to work with a filter stack.
In the "SYNOPSIS", POE::Filter::Line and POE::Filter::Grep are combined into one filter that only returns a particular kind of line. This
can be more efficient than filtering lines in application space, as fewer events may need to be dispatched and handled.
Internally, filters are stored in an array.
Data added by get_one_start() will flow through the filter array in increasing index order. Filter #0 will have first crack at it,
followed by filter #1 and so. The get_one() call will return an item after it has passed through the last filter.
put() passes data through the filters in descending index order. Data will go through the filter with the highest index first, and put()
will return the results after data has passed through filter #0.
PUBLIC FILTER METHODS
In addition to the usual POE::Filter methods, POE::Filter::Stackable also supports the following.
new
By default, new() creates an empty filter stack that behaves like POE::Filter::Stream. It may be given optional parameters to initialize
the stack with an array of filters.
my $sudo_lines = POE::Filter::Stackable->new(
Filters => [
POE::Filter::Line->new(),
POE::Filter::Grep->new(
Put => sub { 1 }, # put all items
Get => sub { shift() =~ /sudo[d+]/i },
),
]
);
pop
Behaves like Perl's built-in pop() for the filter stack. The highest-indexed filter is removed from the stack and returned. Any data
remaining in the filter's input buffer is lost, but an application may always call "get_pending" in POE::Filter on the returned filter.
my $last_filter = $stackable->pop();
my $last_buffer = $last_filter->get_pending();
shift
Behaves like Perl's built-in shift() for the filter stack. The 0th filter is removed from the stack and returned. Any data remaining in
the filter's input buffer is passed to the new head of the stack, or it is lost if the stack becomes empty. An application may also call
"get_pending" in POE::Filter on the returned filter to examine the filter's input buffer.
my $first_filter = $stackable->shift();
my $first_buffer = $first_filter->get_pending();
push FILTER[, FILTER]
push() adds one or more new FILTERs to the end of the stack. The newly pushed FILTERs will process input last, and they will handle output
first.
# Reverse data read through the stack.
# rot13 encode data sent through the stack.
$stackable->push(
POE::Filter::Map->(
Get => sub { return scalar reverse shift() },
Put => sub { local $_ = shift(); tr[a-zA-Z][n-za-mN-ZA-M]; $_ },
)
);
unshift FILTER[, FILTER]
unshift() adds one or more new FILTERs to the beginning of the stack. The newly unshifted FILTERs will process input first, and they will
handle output last.
filters
filters() returns a list of the filters inside the Stackable filter, in the stack's native order.
Calling "<$filter_stack-"filters()>> in the "SYNOPSIS" would return a list of two filter objects:
POE::Filter::Line=ARRAY(0x8b5ee0)
POE::Filter::Grep=ARRAY(0x8b5f7c)
filter_types
filter_types() returns a list of class names for each filter in the stack, in the stack's native order.
Calling "<$filter_stack-"filter_types()>> in the "SYNOPSIS" would return a list of two class names:
POE::FIlter::Line
POE::Filter::Grep
It could easily be replaced by:
my @filter_types = map { ref } $filter_stack->filters;
SEE ALSO
POE::Filter for more information about filters in general.
Specific filters, amongst which are: POE::Filter::Block, POE::Filter::Grep, POE::Filter::HTTPD, POE::Filter::Line, POE::Filter::Map,
POE::Filter::RecordBlock, POE::Filter::Reference, POE::Filter::Stream
BUGS
None currently known.
AUTHORS & COPYRIGHTS
The Stackable filter was contributed by Dieter Pearcey. Documentation provided by Rocco Caputo.
Please see the POE manpage for more information about authors and contributors.
perl v5.14.2 2012-05-15 POE::Filter::Stackable(3pm)