1

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 | 
user1648825
  • 979
  • 11
  • 19
  • A much better fix is to change the program which produced these tables to not decorate the tables. This looks like basically a duplicate of http://stackoverflow.com/questions/9934264/how-to-hide-result-set-decoration-in-psql-output – tripleee Oct 21 '16 at 06:07
  • I have no access to the program. It is 3rd party one. – user1648825 Oct 21 '16 at 08:31
  • It certainly looks like it's calling Postgres behind the scenes. You could experiment with a wrapper to shadow `psql` or something similar, but this is going far outside the scope of your original question already. – tripleee Oct 21 '16 at 08:54

1 Answers1

4

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.)

chepner
  • 497,756
  • 71
  • 530
  • 681