3

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

Community
  • 1
  • 1
prolink007
  • 33,872
  • 24
  • 117
  • 185
  • 3
    Why don't you simply test for the existence of that file instead? – Mat Oct 22 '12 at 14:17
  • 1
    Erm... you mean `grep -E '^init$'` doesn't work? – raina77ow Oct 22 '12 at 14:18
  • If the issue is to avoid having the dot meaning any character, you could escape it with a backslash as `\\.` or perhaps use `fgrep` – Basile Starynkevitch Oct 22 '12 at 14:19
  • As a matter of fact, I think `test -e init` would be even more straightforward. Will `adb shell test -e /init` return anything? ) – raina77ow Oct 22 '12 at 14:22
  • GREP is an acronym. It stands for "Global Regular Expression Print". If you "grep" for a fixed string, you're wasting resources. [What are you REALLY trying to do](http://mywiki.wooledge.org/XyProblem)? – ghoti Oct 22 '12 at 14:24
  • @raina77ow: `adb shell ls / | grep -e /init` did not return anything. – prolink007 Oct 22 '12 at 14:27
  • 1
    @prolink007 - `grep -e /init` does NOT mean the same thing as `grep -E '^init$'` or `test -e /init`. – ghoti Oct 22 '12 at 14:29
  • @ghoti: I have tried both of those as well, earlier, and neither worked. Still nothing is being returned. – prolink007 Oct 22 '12 at 14:30
  • You can't mix and match one command with the options of another command. Tell us what you are trying to achieve, and we'll try to help. If all you want is to see if `/init` exists, then `test -e /init` is the way to go. Beyond that, we need to know your goal before we can give you an appropriate answer. – ghoti Oct 22 '12 at 14:31
  • @ghoti: My goal is clearly stated in my question. And i am not mixing and matching. I am doing exactly as people are posting and seeing no result returned. Thank you for your assistance. – prolink007 Oct 22 '12 at 14:32
  • 1
    @prolink007 - no, you've stated what you're trying to achieve, not why you're trying to achieve it. The way to determine whether a file called "init" exists in your root directory is the `test` command, not grep. And you specifically told us that you were trying your command line with the options for `test` applied to `grep`. Again: tell us what you're really trying to achieve. You've only asked us to help you implement your solution to whatever problem you're facing. – ghoti Oct 22 '12 at 14:38
  • @ghoti: You said, "Tell us what you are trying to achieve" initially. I will answer your new question of "why you're trying to achieve it". And i did answer "Tell us what you are trying to achieve" in my question. I will add the additional information to my question. – prolink007 Oct 22 '12 at 14:42
  • @prolink007 - my mistake, I should have been more explicit in my request for a bigger pocture. I figured that the link to the "XY Problem" page would be a sufficient hint for you. – ghoti Oct 22 '12 at 14:43
  • just curious, why don't you use `if [ -f init ]` as opposed to piping `ls` to `grep`? – doubleDown Oct 22 '12 at 15:55
  • @doubleDown: Because i am not looking on the file system of my computer. I am looking on the file system of the android device using the `ADB`. I do not believe that will work through the `ADB`. – prolink007 Oct 22 '12 at 16:08

4 Answers4

4

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$'
Michael Krelin - hacker
  • 138,757
  • 24
  • 193
  • 173
0

To get an exact match you have to activate regular expressions with the -E parameter and use beginning of line and end of line symbols:

grep -E "^init$"
alestanis
  • 21,519
  • 4
  • 48
  • 67
  • 2
    Nah, you don't need to activate extended regex for `^$`. Doesn't do any harm either. – Michael Krelin - hacker Oct 22 '12 at 14:20
  • This is not working. I am performing this operation on an android device through the `ADB`. Check my additional information in the post. – prolink007 Oct 22 '12 at 14:23
  • Since I commented on the answer, I feel obliged to say I did NOT downvote ;-) But the reason might be your misleading wording — you don't need `-E` to enable regex for grep, `-E` enables *extended* regex. – Michael Krelin - hacker Oct 22 '12 at 16:53
0

Are there problems with grep? How about awk?

ls | awk '$1 == "init"'
Birei
  • 35,723
  • 2
  • 77
  • 82
0

The question is which shell (the device or your pc) you want to do shell expansion/grepping/etc?

If you want your device to do the processing, the command should be

adb shell "ls / | grep ^init$"

If you want your pc to do the job, the command would be

adb shell "ls /" | tr -d '\r' | grep ^init$

But then again, as many others suggested, when you know the exact file name - just check if it exists, no need for grepping

adb shell "[ -f '/init' ] && echo 'found'"
Alex P.
  • 30,437
  • 17
  • 118
  • 169