Posted on 09-21-2020 03:54 PM
Hey all!
I am trying to get part of a line that prints out when I echo is in terminal.
currently it prints link this,
returned:first.last
I would like it to print like this,
first.last
I've tried awk print but it counts the whole line as one column.
I've tried to grep -v returned:
but I end up with nothing
I've tried tr -d returned:
and I get
firs.las
basically it is removing those letters from everywhere in the printout period and not just in that order or that word.
how can I do this?
Note: first.last is just an example. This will be an employee name.
Posted on 09-21-2020 07:59 PM
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
Posted on 09-21-2020 08:03 PM
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