简体   繁体   English

如何通过结合使用grep和wc打印文件中最长的单词

[英]How to print the longest word in a file by using combination of grep and wc

iam trining to find the longest word in a text file. iam trining在文本文件中找到最长的单词。 i tried it and find out the no of characters in the longest word in a file by using the command 我尝试了一下,并使用以下命令找出了文件中最长单词的字符数

wc -L

i need to print the longest word By using this number and grep command . 我需要通过使用此数字和grep命令来打印最长的单词。

If you must use the two commands give, I'd suggest: 如果您必须使用给出的两个命令,建议您:

grep -E ".{$(wc -L < test.txt)}" test.txt

The command substitution is used to build the correct brace expression to match the line(s) with exactly the given number of characters. 命令替换用于构建正确的括号表达式,以将行与恰好具有给定字符数的行进行匹配。 -E is needed to enable extended regular expression support; -E需要启用扩展的正则表达式支持; otherwise, the braces need to be escaped: grep ".\\{...\\}" test.txt . 否则,必须将大括号转义: grep ".\\{...\\}" test.txt

Using an awk command that makes a single pass through the file may be faster. 使用awk命令使文件一次通过可能更快。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM