Sponsored Content
Top Forums Shell Programming and Scripting SED: delete and print the only exact matched pattern Post 302212481 by new_buddy on Monday 7th of July 2008 05:19:42 PM
Old 07-07-2008
SED: delete and print the only exact matched pattern

I am really need help with the regular expression in SED. From input file, I need to extract lines that have the port number (sport or dport) as defined. The input file is something like this
Code:
time=1209515280-1209515340 dst=192.168.133.202 src=208.70.8.23  bytes=2472 proto=6  sport=80 dport=1447
time=1209515280-1209515340 dst=192.168.133.202 src=208.70.8.23  bytes=2526 proto=6  sport=80 dport=1811
time=1209515280-1209515340 dst=192.168.133.202 src=72.14.217.189  bytes=270 proto=6  sport=80 dport=3145
time=1209515280-1209515340 dst=192.168.133.202 src=78.136.25.50  bytes=610 proto=6  sport=80 dport=3158

How can I possibly define the pattern if I only want the exact pattern match. If I only wanted port 4501, is it correct the way I defined in the first line of the sed file below?
The example of patterns in file p2pport-delete.sed are here:
Code:
/port=[4][5][0][1]/d
/port=[4][6][6-7][1-4]/d
/port=[4][6][7][7]/d
.....

Apart from that, I need to have copy of those deleted lines in different file. I am using pattern/p instead. Here is file p2pport-print.sed
Code:
/port=[4][5][0][1]/p
/port=[4][6][6-7][1-4]/p
/port=[4][6][7][7]/p
.....

However, from the output, I found out that the total lines deleted from p2pport-delete.sed and printed from p2pport-print.sed is different. This assure that my sed file is wrong.
I hope that I could get help from anyone that could help me.
Many thanks
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

SED: match pattern & delete matched lines

Hi all, I have the following data in a file x.csv: > ,this is some text here > ,,,,,,,,,,,,,,,,2006/11/16,0.23 > ,,,,,,,,,,,,,,,,2006/12/16,0.88 < ,,,,,,,,,,,,,,,,this shouldnt be deleted I need to use SED to match anything with a > in the line and delete that line, can someone help... (7 Replies)
Discussion started by: not4google
7 Replies

2. Shell Programming and Scripting

Sed to delete exactly match pattern and print them in other file

Hi there, I need help about using sed. Iam using sed to delete and print lines that match the port number as listed in sedfile. I am using -d and -p command for delete match port and print them respectively. However, the output is not synchonize where the total deleted lines is not similar with... (3 Replies)
Discussion started by: new_buddy
3 Replies

3. Shell Programming and Scripting

print last matched pattern using perl

Hi, If there exist multiple pattern in a file, how can I find the last record matching the pattern through perl. The below script searches for the pattern everywhere in an input file. #! /usr/bin/perl -s -wnl BEGIN { $pattern or warn"Usage: $0 -pattern='RE' \n" and exit 255;... (5 Replies)
Discussion started by: er_ashu
5 Replies

4. Shell Programming and Scripting

Print only matched pattern in perl

Hi, I have script like below: #!/usr/local/bin/perl use strict; use warnings; while (<DATA>) { ( my ($s_id) = /^\d+\|(\d+?)\|/ ) ; if ( $s_id == 1 ){ s/^(.*\|)*.*ABC\.pi=(+|+)*.*ABC\.id=(\d+|+).*$/$1$2|$3/s; print "$1$2|$3\n"; (2 Replies)
Discussion started by: sol_nov
2 Replies

5. Shell Programming and Scripting

How to use sed to search a particular pattern in a file backward after a pattern is matched.?

Hi, I have two files file1.txt and file2.txt. Please see the attachments. In file2.txt (which actually is a diff output between two versions of file1.txt.), I extract the pattern corresponding to 1172c1172. Now ,In file1.txt I have to search for this pattern 1172c1172 and if found, I have to... (9 Replies)
Discussion started by: saurabh kumar
9 Replies

6. Shell Programming and Scripting

Delete lines and the first pattern between 2 matched patterns

Hi, i need help to delete all the lines between 2 matched patterns and the first pattern must be deleted too. sample as follows: inputfile.txt >kump_1 ........................... ........................... >start_0124 dgfhghgfh fgfdgfh fdgfdh >kump_2 ............................. (7 Replies)
Discussion started by: redse171
7 Replies

7. Shell Programming and Scripting

Regex: print matched line and exact pattern match

Hi experts, I have a file with regexes which is used for automatic searches on several files (40+ GB). To do some postprocessing with the grep result I need the matching line as well as the match itself. I know that the latter could be achieved with grep's -o option. But I'm not aware of a... (2 Replies)
Discussion started by: stresing
2 Replies

8. UNIX for Advanced & Expert Users

To print from the first line until pattern is matched

Hi I want to print the line until pattern is matched. I am using below code: sed -n '1,/pattern / p' file It is working fine for me , but its not working for exact match. sed -n '1,/^LAC$/ p' file Input: LACC FEGHRA 0 LACC FACAF 0 LACC DARA 0 LACC TALAC 0 LAC ILACTC 0... (8 Replies)
Discussion started by: Abhisrajput
8 Replies

9. Shell Programming and Scripting

awk to delete content before and after a matched pattern

Hello, I have been trying to write a script where I could get awk to delete data before and after a matched pattern. For eg Raw data Start NAME = John Age = 35 Occupation = Programmer City = New York Certification Completed = No Salary = 80000 End Start NAME = Mary Age = 25... (2 Replies)
Discussion started by: sidnow
2 Replies

10. Shell Programming and Scripting

Issue with sed command does not replace exact string matched

I have a file change.sed more change.sed I fire the below command inorder to replace "190.169.11.15" with "10.4.112.240" in proxy.logsed -f change.sed proxy.log proxy.log has the below entry more proxy.log The command replaces both 190.169.11.15 & 190.169.11.155 as below: I am expecting... (17 Replies)
Discussion started by: mohtashims
17 Replies
FILTER 
SYNTAX(5) File Formats Manual FILTER SYNTAX(5) NAME
rules.filter - Input format for filtergen packet filter compiler INTRO
This file describes the input syntax accepted by filtergen(8). BASICS
In general form, a filter rule is described by a direction, an interface, a target and (possibly empty) sets of matches and options. Simple rules will look like: direction interface match0 .. matchN target; for example: input eth0 source host1 dest host2 proto tcp dport http accept; Note that the elements of the rule can be placed in any order, with the exception that the interface must immediately follow the direction. Thus, this rule is equivalent to the above (though perhaps less readable): proto tcp source host1 dport http accept dest host2 input eth0; The semicolon separates rules. It is optional before a closing brace or the end of a file. Whitespace is not significant. Anything after a hash ("#") on a line is ignored. DIRECTION A direction merely specifies whether to match packets being sent or received. The only two directions available are "input" and "output". Forwarded packets will pass through both, INTERFACE This specifies which real or virtual network device to filter. As far as filtergen is concerned, this is just a text string. It must be the same as the device name on the target system. Common names on Linux are "eth0", "eth1", ..., "ppp0", etc. Other systems will have different naming rules. TARGET A target notes what we do with a matching packet. Universal options are accept and drop which, respectively, state that the packet should be allowed as normal, or thrown away. Some backends support reject to throw away a packet, but send notification to the sender that it was denied, masq (on output rules only) to "masquerade" a packet - alter it so that it appears to come from the address of the sending inter- face - and proxy (and its deprecated alias redirect) to divert a connection via the local system. MATCHES The matches are the meat of the rule. They apply a set of tests to a packet and decide if this rule will be used to process it. Available matches are: source addr-range dest addr-range proto {tcp|udp|icmp|...} sport port-range dport port-range icmptype icmp-type Matches can be negated by prefixing them with a "!": input eth0 ! dest 10.0.0.3 reject; (note than not all backends can support this). OPTIONS Options affect the behaviour of the matcher or the target. Currently implemented are log, which logs packets, local, which means only to check packets to or from this interface, forward which means the opposite of local, and oneway which causes the backend to omit rules which permit return packets. The log option can optionally specify a message to log matching packets with, where the backend supports it: input eth0 source { 10.0.0.0/8 192.168.0.0/16 } log text "private addresses" drop; Note that the oneway option make have no effect when used with the -l command-line flag on backends which support it. GROUPING
Because it can get tedious to type: input eth0 source foo dest bar proto tcp dport http accept; input eth0 source foo dest bar proto tcp dport https accept; input eth0 source foo dest bar proto tcp dport nntp accept; input eth0 source foo dest bar proto tcp sport 1:1023 dport ssh accept; ... filter allows you to group rules with a set syntax: input eth0 source foo dest bar proto tcp { dport http; dport https; dport nntp; sport 1:1023 dport ssh; } accept; Matches which accept arguments can also be grouped: input eth0 source foo dest bar proto tcp { dport {http https nntp}; sport 1:1023 dport ssh; } accept; OUT-OF-LINE GROUPS It is commonly the case that both hosts and routers have long lists of similar looking rules to allow traffic between groups of hosts, as above. What if we had another pair of hosts which needed a variety of services? We could simply put the rule groups one after the other: input eth0 source foo dest bar proto tcp { dport {http https nntp}; sport 1:1023 dport ssh; } accept; input eth0 source baz dest quux proto tcp { dport {1264 1521 1984 8008 8080 26000}; } accept; The above generates 11 rules, and every additional port adds another rule through which packets will pass (well, ones which don't match any of the above). The first four output rules have the same source and destination hosts and protocol, and we know that if it doesn't match those on the first rule, it won't on the next three, either. Out-of-line groups use this fact to streamline things somewhat: input eth0 source foo dest bar [ proto tcp { dport {http https nntp}; sport 1:1023 dport ssh; } accept; ]; input eth0 source baz dest quux [ proto tcp { dport {1264 1521 1984 8008 8080 26000}; } accept; ]; Where the underlying system supports it, everything inside the square brackets is moved into a separate "chain" (in ipchains and iptables- speak) or "group" (in ipfilter-speak). Thus, any packet not matching "source foo dest bar" or "source baz dest quux" above will be checked against only two rules, not eleven. Note that matches which must appear together, like "proto tcp" and "sport 12345" need to be either both in the group, or both out of it. EXAMPLE
Here's a fairly complete example, for a single-interface machine: # # Example filter for (for example) a mail server # # Unfortunately, we don't have time to audit the # communications which go on locally {input lo; output lo} accept; # But we want to be a bit more careful when speaking # to the outside world input eth0 { # Sadly, we share a DMZ with Windows machines. # Don't log their netbios noise proto {tcp udp} source ournet/24 dport 137:139 drop; proto tcp { dport { smtp pop-3 } accept; dport ssh source ournet/24 accept; # We don't answer this, but don't want to # cause timeouts by blocking it dport auth reject; log drop; }; # We don't run any UDP (or other non-TCP) # services log drop; }; output eth0 { proto tcp { dport { smtp auth } accept; log drop; }; # Outbound DNS is OK proto udp dport domain dest { ns0 ns1 } accept; log drop; }; SEE ALSO
filtergen(8), filter_backends(7) January 7, 2004 FILTER SYNTAX(5)
All times are GMT -4. The time now is 01:16 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy