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 ofinputfiletoawk'sstdin> outputfile: redirects the content ofawk'sstdouttooutputfile
awk command breakdown:
!/adf\.ly/: prints the record if not matching theadf\.lyregex
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\.lystring/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' fileif 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
iwill 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.lyIf 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
gglobal searchddeletexsave and close
Zombo
- 1
sedand in thegrepversion: in thesedversion you're deleting all the lines containing anad.flysubstring while in thegrepversion you're deleting only lines containing anad.flysubstring surrounded by a non-word character (i.e. not surrounded by[A-Za-z0-9_]); in thegrepcommand also the.matches any character (i.e. it will exclude lines containing e.g.adxfly) and you're not using any of theEREs' syntax in the pattern, so the-Eoption is not really needed. – kos Jun 16 '15 at 09:11sedversion is fine – kos Jun 16 '15 at 09:35grepfor now – Maythux Jun 16 '15 at 09:35grepversion? Just fix it, it can be useful – kos Jun 16 '15 at 09:38sedversion then – kos Jun 16 '15 at 09:47-woption forcesgrepto match onlyad.flysubstring not surrounded by an alphanumerical or_character, so you should drop it if you want to make thegrepversion act the same as yoursedversion, 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,adyflyand so on will be matched. Just use the same pattern used in thesedversion:grep -v "ad\.fly"– kos Jun 16 '15 at 09:54wbut 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.txtfor 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