"sed" doesn't match pattern

Viewed 129

I'm trying to format cut, paste output but sed not working.

file.txt

Apple
Banana
Apple
Banana
Orange
Apple
Orange

code.sh

cut -f2 file.txt | sort | uniq | sed 's/^\|$/#/g'| paste -sd,\& -

expected output / output on ubuntu

#Apple#,#Banana#&#Orange#

getting output / output on macos

Apple,Banana&Orange

Note: The code works on Ubuntu, but on MacOS it doesn't.

4 Answers

This can be done in a single gnu-awk:

awk '!seen[$1]++{} END {
    PROCINFO["sorted_in"]="@ind_str_asc"
    for (i in seen)
      s = s (s == "" ? "" : (++j==1?",":"&")) "#" i "#"
    print s
}' file

#Apple#,#Banana#&#Orange#

On OSX I have gnu awk installed via home brew.

As far as I know, BSD/Mac sed doesn't support \|. See sed not giving me correct substitute operation for newline with Mac - differences between GNU sed and BSD / OSX sed for details.

As an alternate, you can use ERE instead of BRE. I checked it on Linux, apparently this still doesn't seem to work on Mac (See also: MacOS sed: match either beginning or end).

$ echo 'Apple' | sed -E 's/^|$/#/g'
#Apple#

# workaround for Mac
$ echo 'Apple' | sed -e 's/^/#/' -e 's/$/#/'
#Apple#

Instead of sort+uniq+sed, you can also use awk (but note that awk solution shown here removes duplicates while preserving original order, doesn't sort the input):

$ awk '!seen[$0]++{print "#" $0 "#"}' ip.txt
#Apple#
#Banana#
#Orange#

Change $0 to $2 if you want only the second field, based on your use of cut

As mentioned elsewhere, BSD sed doesn't support \|. Instead of replacing ^ and $, you can substitute # around the whole line.

sort -u file.txt | sed 's/.*/#&#/' | paste -sd,'&' -

A simple way to do it using the sed command:

sed -E 's/[[:alnum:]]+/#&#/'
  • the -E option for enabling the POSIX ERE (extended regular
    expression)
  • [[:alnum:]]+ The alphanumeric characters; in ASCII, equivalent to [A-Za-z0-9] with the plus (+) to refer to one or more.
  • the & symbol, does bring or refer to the content of the pattern we found. (on which we surrounded it with #)
Related