Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Bash: How to grep for empty string

  • 15-07-2020 8:38am
    #1
    Registered Users, Registered Users 2 Posts: 5,660 ✭✭✭


    I have an optional argument for a script (a filter) but i want the option to have no filter

    So the command is

    grep $filter file

    I want that filter to sometimes be the empty string i.e everything if the user so wishes but grep '' does not do it


Comments

  • Moderators, Technology & Internet Moderators Posts: 1,336 Mod ✭✭✭✭croo


    I have an optional argument for a script (a filter) but i want the option to have no filter

    So the command is

    grep $filter file

    I want that filter to sometimes be the empty string i.e everything if the user so wishes but grep '' does not do it
    What about
    grep "$filter" file


  • Moderators, Computer Games Moderators, Technology & Internet Moderators Posts: 19,242 Mod ✭✭✭✭L.Jenkins


    Maybe I'm having an off day today, but would using grep to filter an empty string simply seek out and remove all white space within a file or a returned output of a command? As I said, I might be having an off day in thinking this way today.


  • Moderators, Technology & Internet Moderators Posts: 1,336 Mod ✭✭✭✭croo


    L.Jenkins wrote: »
    ...would using grep to filter an empty string simply seek out and remove all white space within a file or a returned output of a command
    No, it returns the full file.

    I've since done a simple test, wrapping it in double quotes and that does work.
    #!/bin/bash
    filter=$1
    grep "$filter" ~/adapter.log
    exit 1
    Passing "" or '' will return all contents of the test file (adapter.log) and passing a value will filter to those lines.


  • Registered Users, Registered Users 2 Posts: 10,910 ✭✭✭✭28064212


    L.Jenkins wrote: »
    Maybe I'm having an off day today, but would using grep to filter an empty string simply seek out and remove all white space within a file or a returned output of a command? As I said, I might be having an off day in thinking this way today.
    grep prints lines matching a pattern. All lines contain the empty string, so it will print all lines.

    Also, note that the empty string is not the same as whitespace

    Boardsie Enhancement Suite - a browser extension to make using Boards on desktop a better experience (includes full-width display, keyboard shortcuts, dark mode, and more). Now available through your browser's extension store.

    Firefox: https://addons.mozilla.org/addon/boardsie-enhancement-suite/

    Chrome/Edge/Opera: https://chromewebstore.google.com/detail/boardsie-enhancement-suit/bbgnmnfagihoohjkofdnofcfmkpdmmce



  • Registered Users, Registered Users 2 Posts: 27,370 ✭✭✭✭GreeBo


    what is your expected output when filter is empty?

    would you not just check for empty and if so, cat the file instead of grepping it?


  • Advertisement
  • Moderators, Technology & Internet Moderators Posts: 1,336 Mod ✭✭✭✭croo


    Well I suspect the grep is not the issue... the issue, as I saw it anyway, was the OP wasn't able to utlise an empty value argument that was passed to his script. It's possible the use of grep was just an example!?


Advertisement