Removing numeric values in certain columns whilst keeping minus signs?

The sed way:

sed -E '
    s/^(([ \t]*-?[ \t]*[0-9.]+[ \t]+[0-9.]+)*)[ \t]+-?[ \t]*[0-9.]+$/\1/;
    s/[0-9.]+[ \t]+([0-9.]+)/\1/g'

Output:

-2  4 -9
 3 -5 -11

The first expression kills the trailing column if there are an odd number of columns. It does that by looking for 0 or more pairs <number> <number>, where the first number can be negative.

Edit: A shorter sed solution, inspired by @mikeserv:

sed -E '
    s/[0-9.]+[ \t]*([0-9.]*)/\1/g;
    s/[- \t]*$//'

The same thing with perl:

perl -lpe 's/^((\s*-?\s*[\d.]+\s*[\d.]+)*)\s+-?\s*[\d.]+$/$1/o; s/[\d.]+\s+([\d.]+)/$1/g'

Another way with perl (probably the cleanest one):

perl -lpe '$a = 1; s/([\d.]+\s*)/$a++ % 2 ? "" : $1/eg; s/[-\s]*$//o'

A perl one:

$ perl -anle 'BEGIN{$,=" "}
  print map{$_=$F[$_]=~/^-/?"-$F[$_+1]":" $F[$_+1]"}grep{!($_%2)}0..$#F' file
-2  4 -9
 3 -5 -11
  • -an split input to @F array
  • BEGIN{$,=" "} set output field separator to a space
  • grep{!($_%2)}0..$#F get all even indexes in @F array, which are indexes of odd elements
  • map{$_=$F[$_]=~/^-/?"-$F[$_+1]":" $F[$_+1]"} check if odd element start with -, then append - to next even element, else append a space

As @terdon's answer but without the sed:

awk '{ for(i=1;i<=NF;i+=2){
         if ($i<0) $(i+1)*=-1;
         $i = "";
       }
       print
     }'