views:

450

answers:

4

I am trying to remove some double quotes (") characters from a text file using a Ruby one liner, with little success.

I have tried the following, and some variations, without success.

ruby -pe 'gsub(/\"/,"")' < myfile.txt

This gives me the following error:

-e:1: Invalid argument - < (Errno::EINVAL)

I am running Ruby on a Win machine:

ruby 1.8.6 (2007-09-24 patchlevel 111) [i386-mswin32]

Any idea?

+1  A: 

How about:

ruby -e 'puts $stdin.read.gsub(34.chr,"")' <myfile.txt
Jonas Elfström
Same error, unfortunately.
Stefano Ricciardi
There's something with backslashes and cmd.exe that doesn't play well.
Jonas Elfström
The new example probably only works to ASCII-files.
Jonas Elfström
This seems to do the trick. What a mouthful though :)
Stefano Ricciardi
A: 
ruby -pe 'gsub(/\"/,"")' myfile.txt
khelll
This doesn't work either. I need the "<" to redirect the input to the script.
Stefano Ricciardi
Sorry but i didn't get "redirect the input to the script"
khelll
What I mean is that the input is coming from a file and that the "<" sign is used to feed the script with that file. Maybe these examples can explain it better: http://www.fepus.net/ruby1line.txt
Stefano Ricciardi
A: 

Sounds like the problem is with the shell.

Your error message is from Ruby, so it seems Ruby is receiving the < as an argument. This means the shell isn't doing any redirection.

I don't have a Windows machine handy so I'd double check that you're getting the redirection right first. On first inspection I think the < myfile.txt should be <myfile.txt

Gareth
This doesn't seem to be the cause. With non special characters it works fine.
Stefano Ricciardi
+4  A: 

Looks like cmd quoting hell -- note that single quotes are meaningless in the cmd shell.

ruby -pe "gsub(34.chr,'')" < filename

but this is probably better:

ruby -pe "$_.delete!(34.chr)" < filename
glenn jackman
They both works. Thanks!
Stefano Ricciardi
This works! must be get accept?
Jirapong