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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sed to delete exactly match pattern and print them in other file
# 1  
Old 07-05-2008
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 the printed lines. I found that sed print lines that match port 4513 although I just want exactly the 4513.
Here is my code
Code:
sed -n -f print.sed tcpfile >> tcpfile.out
sed -f delete.sed tcpfile >>tcpfile.delete

Example of the print.sed
Code:
/451/p
/46[6-7][1-4]/p
...

Example lines in tcpfile
Code:
src=78.136.25.50 dst=192.168.133.202 bytes=610 proto=6 sport=80 dport=3159
src=89.1.65.142 dst=192.168.133.202 bytes=184 proto=6 sport=43954 dport=1153
src=72.14.217.189 dst=192.168.133.202 bytes=270 proto=6 sport=80 dport=3145
src=78.136.25.50 dst=192.168.133.202 bytes=610 proto=6 sport=80 dport=3158
src=208.70.8.23 dst=192.168.133.202 bytes=2472 proto=6 sport=80 dport=1447
src=208.70.8.23 dst=192.168.133.202 bytes=2526 proto=6 sport=80 dport=1811

Really need help. Thanks
# 2  
Old 07-06-2008
Please be a little more clear as to your expectations.

The way you have it written, the "print" operation should print every line that matches a pattern in file print.sed.

Without knowing what is in file delete.sed, it's hard to say what result you will get. If file delete.sed has something like /pattern/p then it will print every line but those with matching patterns will be printed twice. If, however, file delete.sed has something like /pattern/d then it will print every line that does NOT match the pattern. You say you are using -d so that would seem to imply the second case. If your delete.sed file has /pattern/d then what you will have in tcpfile.delete is everything that remains after having already deleted the matching lines. If you want tcpfile.delete to contain the lines that should be deleted, use !d.


Quote:
Originally Posted by new_buddy
I found that sed print lines that match port 4513 although I just want exactly the 4513.
Are you saying that you only want it to print the port number and not the whole line? I'm not clear what you're saying here. If all you want is the port number, and not the entire line, you need more than sed. You also need to use a little caution the way you are specifying your port numbers. The pattern /451/ will match every port that has 451 anywhere in it (451, 1451, 4513, etc). If you make your intentions clearer, it will be easier to help.
# 3  
Old 07-06-2008
Thank you for reply and sorry for my unclear explanation.

Actually, I wanted to have both output which have the only match lines and deleted lines for specified port number in print.sed and delete.sed copied into different files. Port numbers defined in delete.sed and print.sed are similar and only different in term of pattern/d and pattern/p. I tried to use pattern/!d instead of /p but I could not get the output. I could not open the output file and make my disk usage reach 100%.
Apart from that, the problem is the output from both command. It should have similar lines copied and lines deleted but as far as I see, it is not. Then, I figure out that the problem is from the way I defined the port number.
Code:
How to define the port number if I want only lines that match 451, 4661,4662 and so on.
/port=451/p
/port=46[6-7][1-4]/p
...

I hope that I have make a clearer explanation. Thanks.
# 4  
Old 07-10-2008
Sorry for the delay in getting back to you. Work 4-on/4-off and some days I just choose to leave the computer off. Smilie


Ok, you want your tcpfile.delete to actually contain the lines that are (to be) deleted. That's the problem. For this to work, using the !d for the delete, you would have to build a single regular expression that matches every line to be deleted. That is no small task.

A couple of suggestions.

1. Since nothing is really being deleted, and tcpfile.delete really just identifies those lines that need to be deleted, you could use the /p to identify them.

Assuming you're not doing it this way because you want to use the /d, then I have two other suggestions.

2. If you use /d in delete.sed, tcpfile.delete ends up containing the contents of tcpfile minus those lines which would be deleted by the delete expressions. You could then use comm, or some other utility, to compare tcpfile and tcpfile.delete to produce a list of the lines which were deleted. I ran a quick test with comm and, on my system, anyway, it was producing one extra line of output that it shouldn't have been.

3. To generate a file tcpfile.delete that contains the (to be) deleted lines, and using the !d form for delete, you could use multiple delete.sed files.

Code:
 
delete1.sed
/=315[89]$/!d
 
delete2.sed
/=1447$/!d
 
for i in delete*.sed;do sed -f $i tcpfile >> tcpfile.delete;done



There are probably other ways, as well. Just need to decide what's easiest for you. Note the $ in the expressions. That matches the end-of-line in the input. In your last example for matching port 451, you still have the potential to match more than you want (451, 4510, 4511, etc).
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

sed script to delete the last word after a last pattern match

Hi Guys , I am having a file as stated below File 1 sa0 -- i_core/i_core_apb/i_afe0_controller/U261/A sa0 -- i_core/i_core_apb/i_afe0_controller/U265/Z sa1 -- i_core/i_core_apb/i_afe0_controller/U265/A sa1 -- i_core/i_core_apb/i_afe0_controller/U268/Z sa1 -- ... (7 Replies)
Discussion started by: kshitij
7 Replies

2. Shell Programming and Scripting

Egrep patterns in a file and limit number of matches to print for each pattern match

Hi I need to egrep patterns in a file and limit number of matches to print for each matched pattern. -m10 option is not working out in my sun solaris 5.10 Please guide me the options to achieve. if i do head -10 , i wont be getting all pattern match results as output since for a... (10 Replies)
Discussion started by: ananan
10 Replies

3. Shell Programming and Scripting

Match Pattern and print pattern and multiple lines into one line

Hello Experts , require help . See below output: File inputs ------------------------------------------ Server Host = mike id rl images allocated last updated density vimages expiration last read <------- STATUS ------->... (4 Replies)
Discussion started by: tigerhills
4 Replies

4. UNIX for Dummies Questions & Answers

Match Pattern after certain pattern and Print words next to Pattern

Hi experts , im new to Unix,AWK ,and im just not able to get this right. I need to match for some patterns if it matches I need to print the next few words to it.. I have only three such conditions to match… But I need to print only those words that comes after satisfying the first condition..... (2 Replies)
Discussion started by: 100bees
2 Replies

5. Shell Programming and Scripting

Need one liner to search pattern and print everything expect 6 lines from where pattern match made

i need to search for a pattern from a big file and print everything expect the next 6 lines from where the pattern match was made. (8 Replies)
Discussion started by: chidori
8 Replies

6. Shell Programming and Scripting

sed print from last occurrence match until the end of file

Hi, i have file f1.txt with data like: CHECK a b CHECK c d CHECK e f JOB_START .... I want to match the last occurrence of 'CHECK' until the end of the file. I can use awk: awk '/^CHECK/ { buf = "" } { buf = buf "\n" $0 } END { print buf }' f1.txt | tail +2Is there a cleaner way of... (2 Replies)
Discussion started by: ysrini
2 Replies

7. Shell Programming and Scripting

Sed delete blank lines upto first pattern match

Hi Im trying to do the following in sed. I want to delete any blank line at the start of a file until it matches a pattern and then stops. for example: Input output: I have got it to work within a range of two patterns with the following: sed '/1/,/pattern/{/^]*$/d}' The... (2 Replies)
Discussion started by: duonut
2 Replies

8. Shell Programming and Scripting

sed print all lines after pattern match

HiCan someone show me how to print all lines from a file after a line matching a pattern using sed?Thanks (13 Replies)
Discussion started by: steadyonabix
13 Replies

9. Shell Programming and Scripting

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 time=1209515280-1209515340 dst=192.168.133.202 src=208.70.8.23 bytes=2472 proto=6 sport=80 dport=1447... (6 Replies)
Discussion started by: new_buddy
6 Replies

10. 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
Login or Register to Ask a Question