Grep过滤字典 [英] Grep filtering of the dictionary

查看:100
本文介绍了Grep过滤字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难得到一个使用grep的课程,我希望有人能帮助指导我完成这项任务。作业如下。

使用grep打印所有5字母的小写单词,它们的单个字母重复一次(aabbe或ababe无效, a和b在单词中两次)。在该打印旁边,重复的字母按照字母升序排列,购买非重复的字母。



教师指出我们需要使用几个(6)grep语句将结果传送给下一个grep)和一个sed语句(字符串编辑器)来重新格式化最后一组单词,然后将它们传送到一个读取循环,在这里你可以拆开三个非 - dup字母并对它们进行排序。

样本输出:
aback bck
abaft a bft
abase a bes
abash a bhs
abask a bks
减少赌注



我还没有想出如何做更多的打印5个字符的单词,

 grep^ ..... $/ usr / share / dict / words | 


解决方案

所有在一个sed中

  sed -n'
#过滤器5个字母字
/ [a-zA-Z] \ {5 \} / {

#低字母$ b $按/ ABCDEFGHIJKLMNOPQRSTUVWXYZ / abcdefghijklmnopqrstuvwxya /

#单双字母
/\(.\).*\1/!b
/\(.\).*\(.\).*\1.* \1 / b
/\(.\).*\(.\).*\1.*\/ b
/\(.\) * \(.\)。* \ 2。* \ 1 / b

#提取同行和单个
s / \(.\)* \( 。\)\(。* \)\2\(。* \)/ a& \ 2:\ 1 \ 3 \ 4 /
#sort singles
:sort
s /:\([^ a] * \)a\(。* \)$ /:\ 1 \ 2a /
y / abcdefghijklmnopqrstuvwxyz / zabcdefghijklmnopqrstuvwxy /
/ ^ a /!b sort

#clean and print
s / ..//
s /:/ / p
}'YourFile

posix sed在GNU sed上 - posix


I'm having a hard time getting a grasp of using grep for a class i am in was hoping someone could help guide me in this assignment. The Assignment is as follows.

Using grep print all 5 letter lower case words from the linux dictionary that have a single letter duplicated one time (aabbe or ababe not valid because both a and b are in the word twice). Next to that print the duplicated letter followed buy the non-duplicated letters in alphabetically ascending order.

The Teacher noted that we will need to use several (6) grep statements (piping the results to the next grep) and a sed statement (String Editor) to reformat the final set of words, then pipe them into a read loop where you tear apart the three non-dup letters and sort them.

Sample Output:
aback a bck 
abaft a bft 
abase a bes 
abash a bhs 
abask a bks 
abate a bet 

I haven't figured out how to do more then printing 5 character words,

grep "^.....$" /usr/share/dict/words |

解决方案

All in one sed

sed -n '
# filter 5 letter word
/[a-zA-Z]\{5\}/ {

# lower letters
      y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxya/

# filter non single double letter
      /\(.\).*\1/ !b
      /\(.\).*\(.\).*\1.*\1/ b
      /\(.\).*\(.\).*\1.*\2/ b
      /\(.\).*\(.\).*\2.*\1/ b

# extract peer and single
      s/\(.\)*\(.\)\(.*\)\2\(.*\)/a & \2:\1\3\4/
# sort singles
:sort
      s/:\([^a]*\)a\(.*\)$/:\1\2a/
      y/abcdefghijklmnopqrstuvwxyz/zabcdefghijklmnopqrstuvwxy/
      /^a/ !b sort

# clean and print
      s/..//
      s/:/ /p
      }' YourFile

posix sed so --posix on GNU sed

这篇关于Grep过滤字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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