Answers for "bash print fields that begin with string"


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.
Posted by: Guest on July-22-2021

Code answers related to "bash print fields that begin with string"

Code answers related to "Shell/Bash"

Browse Popular Code Answers by Language