Skip to main content
deleted 1 character in body
Source Link
lcd047
  • 7.4k
  • 1
  • 24
  • 33

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*[-\s*$\s]*$//o' 

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*-\s*$//o' 

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' 
added 157 characters in body
Source Link
lcd047
  • 7.4k
  • 1
  • 24
  • 33

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]+\t]*([0-9.]+)|[ \t]*$]*)/\2\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*-\s*$//o' 

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.]+)|[ \t]*$)/\2/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*-\s*$//o' 

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*-\s*$//o' 
added 157 characters in body
Source Link
lcd047
  • 7.4k
  • 1
  • 24
  • 33

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.]+)|[ \t]*$)/\2/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 = 0;1; s/([\d.]+\s*)/$a++ % 2 ? $1"" : ""$1/eg; s/\s*-\s*$//o' 

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.

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:

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

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.]+)|[ \t]*$)/\2/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*-\s*$//o' 
deleted 3 characters in body
Source Link
lcd047
  • 7.4k
  • 1
  • 24
  • 33
Loading
added 3 characters in body
Source Link
lcd047
  • 7.4k
  • 1
  • 24
  • 33
Loading
added 107 characters in body
Source Link
lcd047
  • 7.4k
  • 1
  • 24
  • 33
Loading
added 114 characters in body
Source Link
lcd047
  • 7.4k
  • 1
  • 24
  • 33
Loading
added 83 characters in body
Source Link
lcd047
  • 7.4k
  • 1
  • 24
  • 33
Loading
added 83 characters in body
Source Link
lcd047
  • 7.4k
  • 1
  • 24
  • 33
Loading
added 147 characters in body
Source Link
lcd047
  • 7.4k
  • 1
  • 24
  • 33
Loading
Source Link
lcd047
  • 7.4k
  • 1
  • 24
  • 33
Loading