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
# 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: `,'
sed -i "/\/home\/user\/access-logs/d;/\/home\/user\/mail/d;/\/home\/user\/www/d" filename
1 Like
clx
January 22, 2014, 1:33am
3
How big your file is? You can use grep
also.
$ 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
$ cat file_test
/home/user/bad_link1
/home/user/www
/home/user/mail
/home/user/access_logs
/home/user/bad_link2
#!/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
/home/user/bad_link1
/home/user/bad_link2
if happy with result add -i
Hello,
Here is an awk
utility.
awk '/\/home\/user\/www/ || /\/home\/user\/mail/ || /\/home\/user\/access_logs/ {next} 1' file_name
Output will be as follows.
/home/user/bad_link1
/home/user/bad_link2
Thanks,
R. Singh
1 Like
With those lines in the file this should be sufficient:
awk '!/www/ && !/mail/ && !/access_logs/' file
Thank you all of you. Your solutions work very good
Some more awk
$ awk '!/\/home\/user\/www|\/home\/user\/mail|\/home\/user\/access_logs/' file >my_new_output_file
$ awk ' NR==1{
gsub(/\//,"\\/",dont_print)
gsub(" ","|",dont_print)
}
$0 !~ dont_print
' dont_print="/home/user/www /home/user/mail /home/user/access_logs" file >my_new_output_file
Hello,
If we can be more specific and we want only bad links then following may too help.
awk '/bad/ {print}' file_name
Output will be as follows.
/home/user/bad_link1
/home/user/bad_link2
Thanks,
R. Singh