Low Orbit Flux Logo 2 F

AWK To Delete Lines In A File

Deleting a line in a file is eady using AWK. I wouldn’t necessarily view it as deleting. I would actually view it as filtering or choosing what to include and exclude. To delete a line in a file you would exclude that line and overwrite the original file.

Exclude any line with the string “abc”:


awk ' !/abc/{print}' data.txt

Include only lines with the string “abc”:


awk ' /abc/{print}' data.txt 

Write the output to a new file with the matching lines removed ( deleted ):


awk ' !/abc/{print}' data.txt > new_data.txt

Overwrite the original file effectively deleting matching lines:


awk ' !/abc/{print}' data.txt > data.txt