Linux sed-删除单词不以特定字符开头 [英] Linux sed - Delete words do not start with a specific character

查看:62
本文介绍了Linux sed-删除单词不以特定字符开头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何通过 sed 删除不以特定字符开头的单词?

How to remove words that do not start with a specific character by sed?

示例:

echo "--foo imhere -abc anotherone" | sed ...

结果必须为;

"--foo -abc"

推荐答案

echo "--foo imhere -abc anotherone" |\
sed -e 's/^/ /g' -e 's/ [^-][^ ]*//g' -e 's/^ *//g'

仅当第一个单词可能有误时,才需要first和last -e命令.

The first and last -e commands are needed if only when the first word can be wrong either.

这篇关于Linux sed-删除单词不以特定字符开头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆