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 all,
Vanilla are planning an update to the site on April 24th (next Wednesday). It is a major PHP8 update which is expected to boost performance across the site. The site will be down from 7pm and it is expected to take about an hour to complete. We appreciate your patience during the update.
Thanks all.

Bash: How to grep for empty string

  • 15-07-2020 9:38am
    #1
    Registered Users Posts: 5,528 ✭✭✭


    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,333 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,240 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,333 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 Posts: 10,454 ✭✭✭✭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, and dark mode). Now available through the extension stores

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

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



  • Registered Users Posts: 27,003 ✭✭✭✭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,333 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