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 string adf.ly, ! negates the check so [[ ! $i =~ .*adf\.ly.* ]] will check if the line does not contain adf.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
using
sed
Run this command:
man sed
Using grep
Thanks for @kos notes:
The following will remove the lines containing "adf.ly" in
filename.txt
in-place:Use the above command without
-i
to test it before removing lines.Using
awk
(thanks to terdon for the shortened version):< 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
regexUsing Perl (thanks to terdon for the shortened version):
-n
: places awhile (<>) {[...]}
loop around the script-e
: reads the script from the argumentsPerl 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 lineHere is a
bash
one-liner: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
):You could use the standard text editor,
ed
:You can use Vim in Ex mode:
g
global searchd
deletex
save and close