Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

grep exact match [duplicate]

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.

how-to-grep-the-exact-match

match-exact-word-using-grep

like image 515
prolink007 Avatar asked Mar 25 '26 09:03

prolink007


1 Answers

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$'
like image 66
Michael Krelin - hacker Avatar answered Mar 26 '26 23:03

Michael Krelin - hacker



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!