regex - How to create inverse of scrubbing script in SED? -
i wrote sed script deletes rows ("/d") match set of ip regular expressions. want re-read same source file , create inverse output - delete not in list.
if throw "!" expression, delete since match "not" condition of other ip list entries.
here's example of regex in internal.lst:
/10\.10\.50\.0/d /10\.100\.0\.0/d /10\.101\.0\.0/d /10\.101\.0\.128/d
and example of sed execution (in .bat file):
for /f %%f in ('dir /b source\*.txt') ( sed -f ..\internal.lst staging\%%f > scrubbed\external\%%f rem inverse of above line!!!! >scrubbed\internal\%%f move staging\%%f scrubbed\original )
edit: confirm understand bobbogo's comment, i'd like:
sed -f list.lst staging\in.txt > out.txt
and i'll put in list.lst file:
/10\.100\.0\.0{p;n} /10\.101\.0\.0{p;n} /10\.101\.0\.128{p;n}
is right?
in gnu sed, -n
option suppresses automatic printing of pattern space, allows use p
command print select lines:
$ cat in.txt 10.101.0.128 10.101.0.133 10.101.0.11 $ sed -n '/10\.10\.50\.0/p /10\.100\.0\.0/p /10\.101\.0\.0/p /10\.101\.0\.128/p' in.txt 10.101.0.128
edit:
note approach produce duplicates if input line can match more 1 expressions:
$ cat in.txt 10.101.0.0 10.101.0.128 10.101.0.133 10.101.0.11 $ sed -n '/10\.10\.50\.0/p /10\.100\.0\.0/p /10\.101\.0\.0/p /10\.101\.0\.128/p /10\.101\.0\.[0-9]$/p' in.txt 10.101.0.0 10.101.0.0 10.101.0.128
to deal this, can use p
command, followed d
command:
$ sed -n '/10\.10\.50\.0/{p; d} /10\.100\.0\.0/{p; d} /10\.101\.0\.0/{p; d} /10\.101\.0\.128/{p; d} /10\.101\.0\.[0-9]$/{p; d}' in.txt 10.101.0.0 10.101.0.128
edit:
as per comment bobbogo, can use n
command instead of d
.
Comments
Post a Comment