Linux look命令用于在排序过的文件中查找以指定字符串开头的行。
look [选项] [字符串] [文件]
| 选项 | 说明 |
|---|---|
-a |
使用替代字典文件 /usr/share/dict/words |
-d |
只考虑字母、数字和空格字符 |
-f |
忽略大小写 |
-t |
指定终止字符 |
--help |
显示帮助信息 |
--version |
显示版本信息 |
# 查找以"hello"开头的单词
look hello
# 输出可能包含:
# hello
# hellos
# helloed
# helloing
# 首先确保文件是排序的
sort -o sorted_names.txt names.txt
# 在排序后的文件中查找以"John"开头的行
look John sorted_names.txt
# 查找以"apple"开头的单词,不区分大小写
look -f apple
# 这会匹配 Apple, APPLE, apple 等
# 查找时只考虑字母、数字和空格
look -d "test 123" data.txt
# 查找以"test"开头,以'.'作为终止字符的行
look -t '.' test file.txt
# 创建一个排序的用户名单
echo -e "alice\nbob\ncharlie\ndavid\neve" > users.txt
# 查找以"c"开头的用户名
look c users.txt
# 输出:
# charlie
/usr/share/dict/words