Multiple pattern find and delete line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Multiple pattern find and delete line
# 1  
Old 01-22-2014
Multiple pattern find and delete line

I have a file

Code:
# 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

Code:
# sed -i "/\/home\/user\/access-logs/,/\/home\/user\/mail/,/\/home\/user\/www/ d" /tmp/user_find.txt
sed: -e expression #1, char 49: unknown command: `,'

# 2  
Old 01-22-2014
Code:
sed -i  "/\/home\/user\/access-logs/d;/\/home\/user\/mail/d;/\/home\/user\/www/d"  filename

This User Gave Thanks to pravin27 For This Post:
# 3  
Old 01-22-2014
How big your file is? You can use grep also.

Code:
$ cat t1
/home/user/bad_link1
/home/user/www
/home/user/mail
/home/user/access_logs
/home/user/bad_link2
$
$
$ cat t2
/home/user/www
/home/user/mail
/home/user/access_logs
$
$
$
$ fgrep -v -f t2 t1
/home/user/bad_link1
/home/user/bad_link2

# 4  
Old 01-22-2014
Code:
$ cat file_test 
/home/user/bad_link1
/home/user/www
/home/user/mail
/home/user/access_logs
/home/user/bad_link2

Code:
#!/bin/bash

# Input file
file="file_test"

# Input here whatever you want to delete space will be separator 
delete="/home/user/www /home/user/mail /home/user/access_logs"

delete=$(sed 's/\//\\\//g;s/ /\\|/g' <<<$delete)


sed '/'"$delete"'/d' $file

Resulting
Code:
/home/user/bad_link1
/home/user/bad_link2

if happy with result add -i
# 5  
Old 01-22-2014
Hello,

Here is an awk utility.

Code:
awk '/\/home\/user\/www/ || /\/home\/user\/mail/ || /\/home\/user\/access_logs/ {next} 1' file_name

Output will be as follows.

Code:
/home/user/bad_link1
/home/user/bad_link2


Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 6  
Old 01-22-2014
With those lines in the file this should be sufficient:
Code:
awk '!/www/ && !/mail/ && !/access_logs/' file

# 7  
Old 01-22-2014
Thank you all of you. Your solutions work very good Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

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

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 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... (14 Replies)
Discussion started by: vivek d r
14 Replies

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

4. Shell Programming and Scripting

awk delete/remove rest of line on multiple search pattern

Need to remove rest of line after the equals sign on search pattern from the searchfile. Can anybody help. Couldn't find any similar example in the forum: infile: 64_1535: Delm. = 86 var, aaga 64_1535: Fran. = 57 ex. ccc 64_1639: Feb. = 26 (link). def 64_1817: mar. = 3/4. drz ... (7 Replies)
Discussion started by: sdf
7 Replies

5. Shell Programming and Scripting

sed to find pattern and delete line before and after

I have the following file: line1 line2 MATCH line3 line4 I need to find the pattern, "MATCH" and delete the line before and after MATCH. So the result should be line1 MATCH lline4 I have to use sed because it is the only utility that is common across my environments. I... (1 Reply)
Discussion started by: craftereric
1 Replies

6. UNIX for Dummies Questions & Answers

Find Pattern delete line and next line

I am trying to delete the line with pattern and the next line. Found the below command in forum which also deleted the previous line. how should i modify that to make sure that only the line with pattern and the next line are deleted but not the previous line? awk '/key... (1 Reply)
Discussion started by: rdhanek
1 Replies

7. Shell Programming and Scripting

find pattern, delete line with pattern and line above and line below

I have a file that will sometimes contain a pattern. The pattern is this: FRM CHK 0000 I want to find any lines with this pattern, delete those lines, and also delete the line above and the line below. (4 Replies)
Discussion started by: nickg
4 Replies

8. UNIX for Dummies Questions & Answers

find pattern delete line with pattern and line above and line below

I have a file that will sometimes contain a pattern. The pattern is this: W/D FRM CHK 00 I want to find any lines with this pattern, delete those lines, and also delete the line above and the line below. (1 Reply)
Discussion started by: nickg
1 Replies

9. Shell Programming and Scripting

sed find matching pattern delete next line

trying to use sed in finding a matching pattern in a file then deleting the next line only .. pattern --> <ad-content> I tried this but it results are not what I wish sed '/<ad-content>/{N;d;}' akv.xml > akv5.xml ex, <Celebrant2First>Mickey</Celebrant2First> <ad-content> Minnie... (2 Replies)
Discussion started by: aveitas
2 Replies

10. UNIX for Advanced & Expert Users

Urgent Help required : awk/sed help to find pattern and delete till end of line

Hi, I need help with using an awk or sed filter on the below line ALTER TABLE "ACCOUNT" ADD CONSTRAINT "ACCOUNT_PK" PRIMARY KEY ("ACCT_ID") USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 STORAGE(INITIAL 65536 FREELISTS 1 FREELIST GROUPS 1) TABLESPACE "WMC_DATA" LOGGING ENABLE Look for... (1 Reply)
Discussion started by: rajan_san
1 Replies
Login or Register to Ask a Question