sed command to grep multiple pattern present in single line and delete that line


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
# 1  
sed command to grep multiple pattern present in single line and delete that line

here is what i want to achieve.. i have a file with below contents

Code:
cat fileName
blah blah blah
.
.DROP this
REJECT that
.
 --sport 7800 -j REJECT --reject-with icmp-port-unreachable
 --dport 7800 -j REJECT --reject-with icmp-port-unreachable
.
.
.
more blah blah blah
--dport 3306 -j DROP
.
.
.
blah blah blah
--dport 5070 -j REJECT --reject-with icmp-
port- 5070 -j REJECT --reject-with icmp-port-unreachable
--dport 5070 -j DROP
 5070 -j DROP
.
.blah blah

Now i just want to remove which ever lines which has below things
1> If the line has 7800 and REJECT string
2> If the line has 3306 and DROP string
3> If the line has 5070 and REJECT or DROP String

i tried below things so far

Code:
sed -i '/REJECT\|DROP/d' fileName

but this will remove all DROP and REJECT commands in the file

i also tried below command
Code:
sed -i '/*7800*REJECT*\|*5070*DROP\|REJECT*\|*3306*DROP*/d' fileName

its removing but its failing for 3306 and 5070 cum DROP.

any help on this is appreciated.

Last edited by vivek d r; 05-14-2013 at 03:45 AM..
# 2  
With awk
Code:
awk '!(/7800/ &&  /REJECT/ || /3306/ && /DROP/ || /5070/ && /REJECT|DROP/)' fileName
blah blah blah
.
.DROP this
REJECT that
.
.
.
.
more blah blah blah
.
.
.
blah blah blah
.
.blah blah

# 3  
Is there any way to use this without any redirection to file.. the above command will just display it.. i want it to be saved in the file.
# 4  
Yes, like this
Code:
command bla bla | awk '!(/7800/ &&  /REJECT/ || /3306/ && /DROP/ || /5070/ && /REJECT|DROP/)' >newfile

Just add a pipe | and then the awk command after the command that gives you the output.

If its in a file and you like to store this to a new file, do like this
Code:
awk '!(/7800/ &&  /REJECT/ || /3306/ && /DROP/ || /5070/ && /REJECT|DROP/)' fileName >newfile

Do not do like this, (no need to cat, since awk read file directly)
Code:
cat fileName | awk '!(/7800/ &&  /REJECT/ || /3306/ && /DROP/ || /5070/ && /REJECT|DROP/)' >newfile

# 5  
After piping , the awk command will just thow output on console... it doesnt do any file write operation.
i have a file - fileName after executing the command it should just remove the patterned lines from this file and save it.. and not to display anything on console...
# 6  
What command do you have that gives you the output?`

Test this
Code:
command bla bla | awk '!(/7800/ &&  /REJECT/ || /3306/ && /DROP/ || /5070/ && /REJECT|DROP/) {print | "tee /tmp/newfile"}'

# 7  
not command output.. its just a file with few contents where i want to remove the pattern lines from it.
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #592
Difficulty: Medium
Suppose we want to sort a huge collection of 1 trillion, 10^12, elements. Doing so with Selection Sort or Insertion Sort would require about n^2 = (10^12)^2 = 10^24 or 1 sextillion comparisons.
True or False?

10 More Discussions You Might Find Interesting

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

2. Shell Programming and Scripting

Group Multiple Lines on SINGLE line matching pattern

Hi Guys, I am trying to format my csv file. When I spool the file using sqlplus the single row output is wrapped on three lines. Somehow I managed to format that file and finally i am trying to make the multiple line on single line. The below command is working fine but I need to pass the... (3 Replies)
Discussion started by: RJSKR28
3 Replies

3. Shell Programming and Scripting

Multiple pattern find and delete line

I have a file # cat /tmp/user_find.txt /home/user/bad_link1 /home/user/www /home/user/mail /home/user/access_logs /home/user/bad_link2 I need to delete lines where there are patterns /home/user/www, /home/user/mail and /home/user/access_logs. I used below method, but its throwing error... (8 Replies)
Discussion started by: anil510
8 Replies

4. Shell Programming and Scripting

Multiple pattern match and print the output in a single line

I need to match two patterns in a log file and need to get the next line of the one of the pattern (out of two patterns) that is matched, finally need to print these three values in a single line. Sample Log: 2013/06/11 14:29:04 <0999> (725102) Processing batch 02_1231324 2013/06/11... (4 Replies)
Discussion started by: rpm120
4 Replies

5. Homework & Coursework Questions

sed Multiple Pattern search and delete the line

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: I have file which has got the following content sam 123 LD 41 sam 234 kp sam LD 41 kam pu sam LD 61 Now... (1 Reply)
Discussion started by: muchyog
1 Replies

6. Shell Programming and Scripting

Sed or Grep to delete line containing patter plus extra line

I'm new to using sed and grep commands, but have found them extremely useful. However I am having a hard time figuring this one out: Delete every line containing the word CEN and the next line as well. ie. test.txt blue 324 CEN green red blue 324 CEN green red blue to produce:... (2 Replies)
Discussion started by: rocketman88
2 Replies

7. Shell Programming and Scripting

Need help in sed command [ printing a pattern + its line no or line no alone ]

Hello friends, Only very recently i started learning sed command...an i found that sed is faster in finding the patterns than some of my scripts that uses grep to check the patten inside a file using line by line search method which is time consuming. The below script... (4 Replies)
Discussion started by: frozensmilz
4 Replies

8. Shell Programming and Scripting

grep multiple words in a single line

Hi.. How to search for multiple words in a single line using grep?. Eg: Jack and Jill went up the hill Jack and Jill were best friends Humpty and Dumpty were good friends too ---------- I want to extract the 2nd statement(assuming there are several statements with... (11 Replies)
Discussion started by: anduzzi
11 Replies

9. Shell Programming and Scripting

make multiple line containing a pattern into single line

I have the following data file. zz=aa azxc-1234 aa=aa zz=bb azxc-1234 bb=bb zz=cc azxc-1234 cc=cc zz=dd azxc-2345 dd=dd zz=ee azxc-2345 ee=ee zz=ff azxc-3456 ff=ff zz=gg azxc-4567 gg=gg zz=hh azxc-4567 hh=hh zz=ii azxc-4567 ii=ii I want to make 2nd field pattern matching multiple lines... (13 Replies)
Discussion started by: VTAWKVT
13 Replies

10. Shell Programming and Scripting

single line input to multiple line output with sed

hey gents, I'm working on something that will use snmpwalk to query the devices on my network and retreive the device name, device IP, device model and device serial. I'm using Nmap for the enumeration and sed to clean up the results for use by snmpwalk. Once i get all the data organized I'm... (8 Replies)
Discussion started by: mitch
8 Replies

Featured Tech Videos