Sponsored Content
Top Forums Shell Programming and Scripting SED: delete and print the only exact matched pattern Post 302213032 by new_buddy on Wednesday 9th of July 2008 04:19:49 AM
Old 07-09-2008
Thanks c0mrade. Actually, I want lines that have the defined port number from source file that have 148171 lines. Now I had already get the solution. Here is how I defined the p2pport-delete.sed
Code:
\<[sd]port=2323\>/d
\<[sd]port=46[6-7][1-4]/d
...
...

Same lines go to sed file p2pport-print.sed, only replace /d with /p. I get two output where the files that do not contain p2pport (nonp2pport) and the file that have only p2pport (p2pport). Here are some example:
For nonp2pport:
Code:
time=1209515280-1209515340 src=78.136.25.50 dst=192.168.133.202 bytes=610 proto=6 sport=80 dport=3159
time=1209515280-1209515340 src=89.1.65.142 dst=192.168.133.202 bytes=184 proto=6 sport=43954 dport=1153
time=1209515280-1209515340 src=72.14.217.189 dst=192.168.133.202 bytes=270 proto=6 sport=80 dport=3145
time=1209515280-1209515340 src=78.136.25.50 dst=192.168.133.202 bytes=610 proto=6 sport=80 dport=3158
time=1209515280-1209515340 src=208.70.8.23 dst=192.168.133.202 bytes=2472 proto=6 sport=80 dport=1447
....

Lines in p2pport
Code:
time=1209518700-1209518760 src=78.136.25.50 dst=192.168.133.202 bytes=610 proto=6 sport=80 dport=3306
time=1209523680-1209523740 src=195.5.116.238 dst=192.168.1.220 bytes=62 proto=6 sport=19289 dport=1080
time=1209524460-1209524520 src=78.136.25.50 dst=192.168.133.202 bytes=610 proto=6 sport=80 dport=3531
time=1209525660-1209525720 src=203.110.221.240 dst=192.168.1.220 bytes=62 proto=6 sport=17745 dport=22
time=1209542220-1209542280 src=66.249.93.19 dst=192.168.133.202 bytes=5874 proto=6 sport=80 dport=4242
time=1209544140-1209544200 src=78.136.25.50 dst=192.168.133.202 bytes=610 proto=6 sport=80 dport=4329
.......

However, the print command in sed will print the line twice if it match for sport and dport. For example, in nonp2pport file, the lines deleted are 2175 but lines printed in p2pport are 2176. What can I do if I just want it print once? Hpe anyone could help me with this.
 

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
GREP(1) 						      General Commands Manual							   GREP(1)

NAME
grep - search a file for a pattern SYNOPSIS
grep [ option ... ] pattern [ file ... ] DESCRIPTION
Grep searches the input files (standard input default) for lines (with newlines excluded) that match the pattern, a regular expression as defined in regexp(6). Normally, each line matching the pattern is `selected', and each selected line is copied to the standard output. The options are -c Print only a count of matching lines. -h Do not print file name tags (headers) with output lines. -i Ignore alphabetic case distinctions. The implementation folds into lower case all letters in the pattern and input before interpre- tation. Matched lines are printed in their original form. -l (ell) Print the names of files with selected lines; don't print the lines. -L Print the names of files with no selected lines; the converse of -l. -n Mark each printed line with its line number counted in its file. -s Produce no output, but return status. -v Reverse: print lines that do not match the pattern. Output lines are tagged by file name when there is more than one input file. (To force this tagging, include /dev/null as a file name argument.) Care should be taken when using the shell metacharacters $*[^|()= and newline in pattern; it is safest to enclose the entire expression in single quotes '...'. SOURCE
/sys/src/cmd/grep.c SEE ALSO
ed(1), awk(1), sed(1), sam(1), regexp(6) DIAGNOSTICS
Exit status is null if any lines are selected, or non-null when no lines are selected or an error occurs. GREP(1)
All times are GMT -4. The time now is 03:18 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy