You really do want to use awk for this. Use grep to return the entire line of a page or file that contains something you want to match. Use awk to divide a line into parts and then choose which part you want to return.
What you need to include is a delimiter (also known as a "field separator" and represented as the -F
option) in your awk statement. A delimiter acts like a column divider — think of a comma in a CSV file as the column divider.
The syntax is:
/usr/bin/awk -F : '{ print $2 }'
After the awk command, add the -F
option followed by whatever character or string of characters you want to use as your delimiter. In this case, it's the colon :
. The colon is separating column 1 returned
and column 2 first.last
.
The number in the '{ print $2 }'
piece of the command says which column you want to return.
The full command would look something like:
echo returned:first.last | /usr/bin/awk -F : '{ print $2 }'
first.last
Can you give sample output?
% string="returned:first.last"
% echo "${string}"
returned:first.last
% awk -F: '{ print $2 }' <<< "${string}"
first.last
I had to use a herestring
for this but you could pipe to it probably. I just don't know what your output looks like. In awk
you can specify a field separator with -F
which is what I am doing
% echo "${string}" | cut -d ":" -f 2
first.last
The same thing in cut
you can set the "delimiter" with -d
and then pick which field you want with -f
also shell does this with built-ins if you want to get extra gray beard
% echo "${string#*:*}"
first.last