How do I remove lines of a file (hosts) which contain "adf.ly" string?
Asked
Active
Viewed 7,600 times
6 Answers
13
using sed
Run this command:
sed -i '/adf\.ly/d' inputfile
man sed
-i[SUFFIX], --in-place[=SUFFIX]
edit files in place (makes backup if extension supplied)
Using grep
Thanks for @kos notes:
grep -v "ad\.fly" inputFile > outputfile

Maythux
- 84,289
3
Using awk
(thanks to terdon for the shortened version):
< inputfile awk '!/adf\.ly/' > outputfile
< inputfile
: redirects the content ofinputfile
toawk
'sstdin
> outputfile
: redirects the content ofawk
'sstdout
tooutputfile
awk
command breakdown:
!/adf\.ly/
: prints the record if not matching theadf\.ly
regex
Using Perl (thanks to terdon for the shortened version):
< inputfile perl -ne '/adf\.ly/||print' > outputfile
-n
: places awhile (<>) {[...]}
loop around the script-e
: reads the script from the arguments
Perl command breakdown:
/
: starts the patternadf\.ly
: matches anadf\.ly
string/
stops the pattern||
: executes the following command only if the pattern didn't match the lineprint
: prints the line

kos
- 35,891
-
1Why not just
awk '!/ad\.fly/' in > out
? You could also shorten the Perl toperl -ne '/ad\.fly/||print' file
if you're into golfing :) – terdon Jun 16 '15 at 12:03 -
@terdon Because I'm silly (
awk
) and anyway not that proficient in both languages. Thanks for your suggestions (by the way I didn't get the golfing reference!) – kos Jun 16 '15 at 12:19 -
1Code golfing is the game of making code as concise as possible, usually at the price of sacrificing legibility. My point is that the perl alternative I gave is indeed shorter but yours was much clearer and more understandable to a non-expert. – terdon Jun 16 '15 at 13:11
-
@terdon Ah I see, yes I'd say I am then. After all it's sill explained right after – kos Jun 16 '15 at 13:29
1
Here is a bash
one-liner:
while IFS= read -r i; do [[ ! $i =~ .*adf\.ly.* ]] && echo "$i"; done <file.txt
Variable
i
will contain each line while iterating[[
$i =~ .*adf\.ly.* ]]
checks if the line has the stringadf.ly
,!
negates the check so[[ ! $i =~ .*adf\.ly.* ]]
will check if the line does not containadf.ly
If yes (
&&
), then the line will be printed.
To save the output to another file (out.txt
):
while IFS= read -r i; do [[ ! $i =~ .*adf\.ly.* ]] && echo "$i"; done <file.txt >out.txt

heemayl
- 91,753
-
1+1, just a little suggestion, you can directly pattern match against
adf\.ly
– kos Jun 16 '15 at 12:47
1
You can use Vim in Ex mode:
ex -sc 'g/adf\.ly/d' -cx hosts
g
global searchd
deletex
save and close

Zombo
- 1
sed
and in thegrep
version: in thesed
version you're deleting all the lines containing anad.fly
substring while in thegrep
version you're deleting only lines containing anad.fly
substring surrounded by a non-word character (i.e. not surrounded by[A-Za-z0-9_]
); in thegrep
command also the.
matches any character (i.e. it will exclude lines containing e.g.adxfly
) and you're not using any of theERE
s' syntax in the pattern, so the-E
option is not really needed. – kos Jun 16 '15 at 09:11sed
version is fine – kos Jun 16 '15 at 09:35grep
for now – Maythux Jun 16 '15 at 09:35grep
version? Just fix it, it can be useful – kos Jun 16 '15 at 09:38sed
version then – kos Jun 16 '15 at 09:47-w
option forcesgrep
to match onlyad.fly
substring not surrounded by an alphanumerical or_
character, so you should drop it if you want to make thegrep
version act the same as yoursed
version, which doesn't cater for that (and I guess that's the way it should act, since it looks like OP is trying to skip lines containing links). Also the pattern is wrong, the.
in particular means "any character", i.e. lines containing strings such asadxfly
,adyfly
and so on will be matched. Just use the same pattern used in thesed
version:grep -v "ad\.fly"
– kos Jun 16 '15 at 09:54w
but I dont take care of_
and other special characters, though i fixed it again. thanks – Maythux Jun 16 '15 at 09:57grep -Fv adf.ly input.txt > output.txt
for better performance on very large files. – evilsoup Jun 16 '15 at 17:45grep
, which is IMO the most straightforward way to do this. – Gaurav Jun 16 '15 at 18:33