Skip to main content

Awk

Search and field separator - awk

This returns the users with access to bash

awk -F ':' '/\/bash/ {print $1}' /etc/passwd

-F is the field separator with :
Seaching for /bash in the file

Search and replace

Replace FIRST instances with sub()

Search for the string SEARCH-STR and replace with REPLACE-STR

awk '{sub(/SEARCH-STR/, REPLACE-STR); print}' <file>

sub(); to find first instance of the SEARCH-STR and substitute with REPLACE-STR

Replace ALL instances with gsub()

awk '{gsub(/SEARCH-STR/, REPLACE-STR); print}' <file>

Reference

10x Eng