I need to cycle through an entire file of unknown size and remove any line in which a given word (passed in as argument 1) appears in a specified column. In addition, I need to keep track of how many lines are removed. I'm assuming this is a job for awk, but I'm having a lot of trouble with it. I've tried working with awk match, but in addition to some other syntactical issues, I'm having trouble getting it to translate the argument into a word.
Example (File.txt):
Katie 1234 4567 blue
Ben 3456 2345 purple
Alex 7896 6789 blue
$ script.sh blue 4
Edits file to:
Ben 3456 2345 purple
And outputs: 2 lines removed
I'm more interested in understanding what I'm doing than just getting the code.
Best Answer
Explanations:
-v value="$1"
sets the awk variablevalue
to the shell script's first argument.$column == value
is true, the code in the braces is executed.$column
is the content of the column numbercolumn
(starting at 1).++removed
increments a counter of removed lines. The variable starts at 0.next
skips to the next input line, so that theprint
instruction won't be executed when the condition is true.1 {print}
prints every line that didn't cause thenext
directive to be executed. (1
is an always-true condition.)END {…}
executes the code inside the braces at the end of the input.