Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Bash: How to grep for empty string

  • 15-07-2020 09:38AM
    #1
    Registered Users, Registered Users 2 Posts: 5,761 ✭✭✭


    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,338 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,338 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: 11,089 ✭✭✭✭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,517 ✭✭✭✭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,338 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