Search code examples
bashawksedtouch

sed or awk, removing curly brackets but only if there are no commas inside brackets


i have this string:

ex00/{ft_strdup.c} ex04/{ft_convert_base.c,ft_convert_base2.c} ex05/{ft_split.c}

need to remove with sed the curly brackets if there is no comma inside brackets, so desired output:

ex00/ft_strdup.c ex04/{ft_convert_base.c,ft_convert_base2.c} ex05/ft_split.c

Solution

  • Using any sed:

    $ sed 's/{\([^,}]*\)}/\1/g' file
    ex00/ft_strdup.c ex04/{ft_convert_base.c,ft_convert_base2.c} ex05/ft_split.c
    

    Note that the above will work no matter which characters except ,, {, }, or \n exist in your file names, e.g. these are all valid file names:

    $ cat file
    ex00/{ft_strdup1.c} ex05/{ft-split.c} ex05/{ft=s&pl#it.c}
    
    $ sed 's/{\([^,}]*\)}/\1/g' file
    ex00/ft_strdup1.c ex05/ft-split.c ex05/ft=s&pl#it.c
    

    If your file names can contain any of the characters I mentioned above as excluded then ask a new question including those in your sample input/output.