I want to know the equivalent of grep -oE '[^ ]+$' pathnameto awk or sed.If someone can answer and explain that would be great.Thank you.
Equivalent of grep to awk or sed
awk;sed;grep
Let's go over what your grep command does first:the -o tells grep to output only the matching part instead of the whole linethe -E flag allows use of extended regular expressions'[^ ]+$' will match any non-space character repeated one or more times at the end of the line - basically a word at the end of the line.Test run:$ cat input.txtto be or not to bethat is the question$ grep -oE '[^ ]+$' input.txt                                      bequestionNow, how can we do the same in awk ? Well that's easy enough considering that awk operates by default on space-separated entries of each line (we call them words - awk calls them fields). Thus we could print $NF with awk - take the NF variable for number of fields and treat it as referring to specific one. But notice that the grep command would only match non-blank lines, i.e. there is at least one word there. Thus, we need to a condition for awk - operate only on lines which have NF number of fields above zero.awk 'NF{print $NF}' input.txtIt should be noted that GNU awk at least supports extended regex (I'm not familiar with others as extensively, so won't make claims about others). Thus we could also write a variation on cuonglm's answer:$ awk '{if(match($0,/[^ ]+$/)) print $NF}' input.txt               bequestionWith GNU sed one can use extended regular expressions as well - that requires -r flag, but you can't simply use same regular expression. There's a need to use backreference with \1.$ cat input.txt                                                                                                          to be or not to bethat is the questionblah $ sed -r -e 's/^.* ([^ ]+)$/\1/' input.txt                                                                             bequestionblahIt is possible to obtain desired result with basic regular expression like so:$ cat input.txt                                                    to be or not to bethat is the questionblah $ sed 's/^.* \([^ ].*\)$/\1/' input.txt                           bequestionblah For more info, please refer to these posts:can we print the last word of each line in linux using sed command?https://unix.stackexchange.com/a/31479/85039