user1648825
user1648825

Reputation: 999

sed ubuntu vs mac regexp

This regexp i use on ubuntu with sed '1p;/^\s/!d;/^\s*id/d'. It lefts the first line untouched, removes empty lines and all, that don't begin with '{space}' or begin with '{blank}id'. On mac it lefts only the first line. How should regexp looks like on mac? Example test.csv

   id  | code | color_code | 
-------+------+------------+
 11A00B|  15  | 9129102    |
 11A00C|  16  | 9129103    |
(2 rows)

   id  | code | color_code | 
-------+------+------------+
 11B00B|  25  | 9129152    |
 11B00C|  36  | 9129162    |
(2 rows)

   id  | code | color_code | 
-------+------+------------+
 11C00B|  22  | 9129107    |
 11C00C|  9   | 9129108    |
(2 rows)

After I call sed -i '1p;/^\s/!d;/^\s*id/d' test.csv on ubuntu

   id  | code | color_code | 
 11A00B|  15  | 9129102    |
 11A00C|  16  | 9129103    |
 11B00B|  25  | 9129152    |
 11B00C|  36  | 9129162    |
 11C00B|  22  | 9129107    |
 11C00C|  9   | 9129108    |

on mac

   id  | code | color_code | 

Upvotes: 1

Views: 230

Answers (1)

chepner
chepner

Reputation: 531165

Ubuntu uses GNU sed, while OS X uses BSD sed. The quick answer is that \s is a GNU extension to recognize whitespace; use the standard [[:space:]] in its place and your command will work on both.

sed -i "bak" '1p;/^[[:space:]]/!d;/^[[:space:]]*id/d' test.csv

(Another difference is that BSD sed requires a suffix with the -i option, while it is optional with GNU.)

Upvotes: 4

Related Questions