How do I split a string on a delimiter ` in Bash?

喜夏-厌秋 提交于 2019-12-08 05:52:04

问题


How can I get modules/bx/motif only on the following through pipeline?

$ find . | grep denied
find: `modules/bx/motif': Permission denied

回答1:


Simply this, using sed:

find . 2>&1 | sed 's/^[^:]*: .\(.*\).: Permission denied/\1/p;d'

or by using bash only,

As your question stand for bash:

string=$'find: `modules/bx/motif\047: Permission denied'
echo $string 
find: `modules/bx/motif': Permission denied

part=${string#*\`} 
echo ${part%\'*}
modules/bx/motif



回答2:


You can redirect STDOUT (where the errors you want appear) so you can process it with other tools, then throw away STDIN (which contains non-errors that you don't care about) then use cut (or any of a hundred other methods) to pull out the bits you need:

find . 2>&1 >/dev/null | cut -d"‘" -f2 | cut -d"’" -f1

You might also be able to use one of the built-in find filters (like -perm maybe) to pick out these files, but you'll have to check the findq man page for details.

find . -not -readable might be helpful in your case.



来源:https://stackoverflow.com/questions/49431602/how-do-i-split-a-string-on-a-delimiter-in-bash

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!