Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR SHELL

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.
Source by unix.stackexchange.com #
 
PREVIOUS NEXT
Tagged: #bash #print #fields #string
ADD COMMENT
Topic
Name
7+8 =