问题描述
我需要的grep
用于从 LS
命令结果的列表上的精确匹配。
例如,如果我 LS
一个目录,这里的结果:
系统
SYS
sbin目录
PROC
init.rc
init.mahimahi.rc
init.goldfish.rc
在里面
我想的grep
来看看文件是否存在名为INIT。我需要的的grep
来只返回一个结果。只有一个在列表中命名为初始化的文件。
我在Android设备上执行此操作。因此,这里是完整的命令:
亚行外壳LS / | grep的-E^ $初始化
我需要检查的Android设备上的目录的存在,那么如果目录存在执行操作。
我在一个批处理脚本中使用找到
这样做过,但我需要使用要做到这一点在Linux 庆典
,我想完全匹配。这将避免这个问题,如果有包含字符串我正在寻找另一个目录或文件。
我已经尝试了建议,以下链接,但他们都没有返回结果当我给初始化作为参数。
how-to-grep-the-exact-match
match-exact-word-using-grep
假设LS文件没有任何与文件系统做(这样就可以不使用文件系统来代替),的grep'^初始化$
应该做的伎俩。
XXD结果:
00000a0:2e67 6f6c 6466 6973 7263 682e 696e 0D0A .goldfish.rc..in
00000b0:6974 0D0A 6465 6661 756c 742e 7072 6f70 it..default.prop
在此基础上hexdump都可以,我建议你首先把它转化为UNIX风格:
亚行外壳LS / | TR -d'\\ 015'| grep'可以^ $初始化
I need to grep
for an exact match on a list of results from an ls
command.
For example, if i ls
a directory and here are the results:
system
sys
sbin
proc
init.rc
init.mahimahi.rc
init.goldfish.rc
init
I would like to grep
to see if a file exists that is named "init". I need the grep
to only return the one result. There is only one file named "init" in the list.
I am performing this operation on an android device. So here is the full command:
adb shell ls / | grep -E "^init$"
I need to check the existence of a directory on the android device and then perform an action if the directory exists.
I have done this before using find
in a batch script, but i need to do this on linux using bash
and i want an exact match. This will avoid the issue if there is another directory or file that contains the string i am searching for.
I have tried the suggestions in the following links, but they all return no result when i give "init" as the parameter.
Assuming the ls file has nothing to do with the filesystem (so that you can't use the filesystem instead), grep '^init$'
should do the trick.
xxd results:
00000a0: 2e67 6f6c 6466 6973 682e 7263 0d0a 696e .goldfish.rc..in
00000b0: 6974 0d0a 6465 6661 756c 742e 7072 6f70 it..default.prop
based on this hexdump, I'd suggest that you turn it to unix-style first:
adb shell ls /|tr -d '\015'|grep '^init$'
这篇关于grep的精确匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!