Results 1 to 4 of 4

Thread: sed yes

  1. #1
    Join Date
    Dec 2006
    Beans
    794

    sed yes

    Essentially, I would like to use sed to replace the whitespaces with underscores in the "yes" lines only.

    Changing this...
    Code:
    var x = [
    {
    "yes":"a b c",
    "no":"a b c"
    },
    {
    "yes":"d e f g h",
    "no":"d e f g h"
    }
    ];
    ...to this:
    Code:
    var x = [
    {
    "yes":"a_b_c",
    "no":"a b c"
    },
    {
    "yes":"d_e_f_g_h",
    "no":"d e f g h"
    }
    ];
    Thanks for any guidance!

  2. #2
    Join Date
    Jul 2005
    Location
    I think I'm here! Maybe?
    Beans
    Hidden!
    Distro
    Xubuntu 22.04 Jammy Jellyfish

    Re: sed yes

    The command
    Code:
    sed 's/\ /_/g' filename
    would remove spaces from all lines of a file but it looks as if you can act only on lines matching a regex according to man sed
    Code:
    \cregexpc
                  Match lines matching the regular expression regexp.  The c may be any character.
    I've never used this but I do use the 's/\ /_/g' part of the sed command with rename command to remove spaces from any file or folder names in my home using one of the many custom-actions in my thunar file-manager.

    Spaces in file/folder names are definitely bad news so I always replace them with an underscore!

    EDIT:
    I've just tried to figure this out but failed and my bash-foo is not good enough to sort out why; probably my lack of understanding of regex!
    Hopefully this might give you some clues to further searching about the job you need done.
    Last edited by ajgreeny; December 4th, 2020 at 08:57 PM.

  3. #3
    Join Date
    Jan 2017
    Beans
    235

    Re: sed yes

    Code:
    sed '/yes/ s/\ /_/g' filename
    The above uses a regex (/yes/) as an address, see the man page under 'Addresses'. If the regex matches the substitution is performed.

  4. #4
    Join Date
    Dec 2006
    Beans
    794

    Re: sed yes

    sed yes, indeed!
    Code:
    sed '/yes/ s/\ /_/g' filename
    This worked!

    Thank you both!

    Two handy sandbox references:
    https://regex101.com/
    https://sed.js.org/

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •