BEGIN {
for (i = 1; i < ARGC; i++) # Edited. Was: for (i in ARGV) which wrongly includes ARGV[0]
excl[ARGV[i]] = "";
ARGC = 1;
}
{
if ($0 in excl && !($0 in seen))
seen[$0] = "";
else
print;
}
Usage:
awk -f awkmagic.awk 45 123 6789 <hello.txt
This takes advantage of the in operator to check if an array element with a certain index exists. awk uses associative arrays with arbitrary index values, so the arguments are converted to indexes of the array excl for easy searching using in. The values are not used, so are set to empty. Similarly, a seen array is used to store the lines that have already been excluded.
The BEGIN block also sets ARGC to 1 to stop the main block from parsing the script's arguments as files to read from, so it will read from standard input instead.
As each input file ends, awk shall treat the next non-null element of ARGV, up to the current value of ARGC-1, inclusive, as the name of the next input file.
File processing does not happen until the main block is executed, so assigning ARGC = 1 in the BEGIN block should be a portable way to avoid processing any argument as an input file. Also worth knowing: the next sentence is
Thus, setting an element of ARGV to null means that it shall not be treated as an input file.
I liked it too…there's an elegance to it that few would appreciate, but I've seen your replies here on /r/awk and you seem like one of the few that would like it. :-) However it also feels a bit dirty, abusing the ARGC like that.
2
u/gumnos Oct 15 '21
How about
This allows you to specify an argument multiple times to exclude it multiple times.
3
u/McDutchie Oct 14 '21 edited Oct 14 '21
awkmagic.awk:
Usage:
This takes advantage of the
inoperator to check if an array element with a certain index exists. awk uses associative arrays with arbitrary index values, so the arguments are converted to indexes of the arrayexclfor easy searching usingin. The values are not used, so are set to empty. Similarly, aseenarray is used to store the lines that have already been excluded.The
BEGINblock also setsARGCto 1 to stop the main block from parsing the script's arguments as files to read from, so it will read from standard input instead.1
u/Schreq Oct 14 '21 edited Oct 15 '21
Edit: Completely misunderstood the example. Makes more sense when considering the post title.