sed:删除包含字符类的整个单词 [英] sed: remove whole words containg a character class

查看:75
本文介绍了sed:删除包含字符类的整个单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从文本文件中删除任何包含非字母字符的单词.例如

I'd like to remove any word which contains a non alpha char from a text file. e.g

"ok 0bad ba1d bad3 4bad4 5bad5bad5"

应该成为

"ok"

我尝试使用

echo "ok 0bad ba1d bad3 4bad4 5bad5bad5" | sed 's/\b[a-zA-Z]*[^a-zA-Z]\+[a-zA-Z]*\b/ /g'

推荐答案

使用 awk :

s="ok 0bad ba1d bad3 4bad4 5bad5bad5"
awk '{ofs=""; for (i=1; i<=NF; i++) if ($i ~ /^[[:alpha:]]+$/)
         {printf "%s%s", ofs, $i; ofs=OFS} print ""}' <<< "$s"
ok

awk 命令循环遍历所有单词,如果单词与正则表达式/^ [[:alpha:]] + $/匹配,则将其写入标准输出.如果当前字段编号小于 NF ,则(i< NF)?OFS:RS 是添加 OFS 的捷径,否则将写入> RS .

This awk command loops through all words and if word matches the regex /^[[:alpha:]]+$/ then it writes to standard out. (i<NF)?OFS:RS is a short cut to add OFS if current field no is less than NF otherwise it writes RS.

一起使用 grep + tr :

s="ok 0bad ba1d bad3 4bad4 5bad5bad5"
r=$(grep -o '[^ ]\+' <<< "$s"|grep '^[[:alpha:]]\+$'|tr '\n' ' ')
echo "$r"
ok

第一个 grep -o 将字符串分成单个单词.2nd grep仅搜索仅具有字母的单词.最后, tr \ n 转换为空格.

First grep -o breaks the string into individual words. 2nd grep only searches for words with alphabets only. ANd finally tr translates \n to space.

这篇关于sed:删除包含字符类的整个单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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