Cannot sort a file in ruby but can in bash

I have a file, x.txt, of lines where a typical line is:

7|22|Come let us praise the Lord

Currently it is ordered on the first column and I want to order it on
the third column, where columns are separated by ‘|’. At the terminal I
type the following line and I get what I want in y.txt.

sort -t| -k 3,3d x.txt -o y.txt

In ruby I tried both the following and for neither is y.txt created.

system(“sort -t| -k 3,3d x.txt -o y.txt”)
res = sort -t\| -k 3,3d x.txt -o y.txt

In the second of these, res = “” after running it and there is the
following message at the terminal:

sh: -k: command not found
sort: option requires an argument – t
Try `sort --help’ for more information.

I tried several things mentioned in ‘sort --help’ but got no nearer.

If anyone can help at all, I’d be very grateful. I’m on an iMac running
OS X v. 10.6.8 and Ruby 1.9.3.

Barrie.

Dear Barrie Stott,

system(“sort -t\| -k 3,3d x.txt -o y.txt”)
system(‘sort -t| -k 3,3d x.txt -o y.txt’)
%x[sort -t\| -k 3,3d x.txt -o y.txt]
sort -t\\| -k 3,3d x.txt -o y.txt

All 4 above will work.
Don’t expect them to return the sorted file because your command is
telling to write stdout to y.txt.
So the “res” variable will be “” if it works.
Just check if the y.txt is created and it’s sorted.

It’s all about single and double quoted strings and how they handle
string interpolation in Ruby.

puts ‘a\nb’

it will print literally

a\nb

puts “a\nb”

it will print, with a newline character between them.

a
b

Kind regards,
Abinoam Jr.

You’re welcome!

Abinoam Jr.
Em 01/02/2014 12:30, “Barrie Stott” [email protected] escreveu:

On 1 Feb 2014, at 11:01, Abinoam Jr. wrote:

So the “res” variable will be “” if it works.
puts “a\nb”

Barrie.
Many thanks, Abinoam Jr., for your speedy reply. I’m sorry I didn’t
reply earlier but I’ve been taking an 83 year old lady out for lunch
because it was her birthday and have only just returned.

I tried the last example you gave me and it worked without a hitch so
I’ll try the others as well and read up on ‘single and double quoted
strings and how they handle string interpolation in Ruby’. It should
stand me in good stead for the future.

I’m really grateful and no reply is needed.

Barrie.