bash print fields that begin with string 1

bash print fields that begin with string

# Basic syntax:
awk '{ for(i=1; i<=NF; i++) { if($i ~ /^string/) { print $i }}}' your_file
# Where:
#	- for(i=1; i<=NF; i++) - this loops through the fields in each row of
#		your_file, starting at field 1 and ending at the number of fields
#		NF in the current row. Without using a custom delimiter, awk
#		treats spaces, tabs, and new lines as delimiters.
#	- if($i ~ /^string/) - this checks if $i (the current field) begins
#		with "string". The tilde character (~) is shorthand for "check 
#		the operands on either side to see if they match". /^string/ is
#		a regex expression where ^ indicates the beginning of the text.
#		If the if statement is true, the action (print $i) is performed.

Here is what the above code is Doing:
1. Read in the file
2. For each line in the file, split the line into fields
3. For each field in the line, check if the field begins with "string"
4. If the field begins with "string", print the field

Similar Posts