# Parse/Split \`cmd\`

**URL:** <https://www.ruby-forum.com/t/parse-split-cmd/64750>\
**Category:** Ruby\
**Created:** [August 15, 2006, 2:18pm UTC](https://www.ruby-forum.com/t/parse-split-cmd/64750 "2006-08-15T14:18:36Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![tjones](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/898d66/32.png) [@tjones](https://www.ruby-forum.com/u/tjones)\
**Post date:** [August 15, 2006, 2:18pm UTC](https://www.ruby-forum.com/t/parse-split-cmd/64750/1 "2006-08-15T14:18:36Z")

</div>

Hello,  
I’m really new to Ruby, I have been using TCL for a really long time  
and I just picked up the “Programming Ruby” book and I really like it.  
One very simple thing I do a lot in my TCL scripts is parse the output  
from a cmd it would read [split $x \n].

OK, I have a def defined to run my command line app and I want to split  
the result at the line ending, how do I do it? I have tried split(\n)  
and I get a error.

Thanks,  
tom

---

<div class="post-metadata">

**Author:** ![tjones](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/898d66/32.png) [@tjones](https://www.ruby-forum.com/u/tjones)\
**Post date:** [August 15, 2006, 2:18pm UTC](https://www.ruby-forum.com/t/parse-split-cmd/64750/2 "2006-08-15T14:18:36Z")

</div>

-----BEGIN PGP SIGNED MESSAGE-----  
Hash: SHA1

tjones wrote:

> One very simple thing I do a lot in my TCL scripts is parse the output  
> from a cmd it would read [split $x \n].

Use $x.split, which works on newlines by default.

> OK, I have a def defined to run my command line app and I want to split  
> the result at the line ending, how do I do it? I have tried split(\n)  
> and I get a error.

irb(main):001:0\> s = “foo\nbar”  
=\> “foo\nbar”

irb(main):002:0\> s.split  
=\> [“foo”, “bar”]

irb(main):003:0\> s.split “\n”  
=\> [“foo”, “bar”]

irb(main):004:0\> s.split ‘\n’  
=\> [“foo\nbar”]

-----BEGIN PGP SIGNATURE-----  
Version: GnuPG v1.4.2.2 (GNU/Linux)

iD8DBQFE3oG1mV9O7RYnKMcRArdTAKCVfaDz+7c+7NEmANm0Jmyf3bmeFgCgk5SP  
r210B4lLIt295WThfDYoQdU=  
=WWlR  
-----END PGP SIGNATURE-----

---

<div class="post-metadata">

**Author:** ![tjones](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/898d66/32.png) [@tjones](https://www.ruby-forum.com/u/tjones)\
**Post date:** [August 15, 2006, 2:18pm UTC](https://www.ruby-forum.com/t/parse-split-cmd/64750/3 "2006-08-15T14:18:37Z")

</div>

Hi,

At Sun, 13 Aug 2006 10:30:10 +0900,  
tjones wrote in [ruby-talk:208085]:

> One very simple thing I do a lot in my TCL scripts is parse the output  
> from a cmd it would read [split $x \n].

lines = `cmd`.to\_a

lines = IO.popen(“cmd”) {|f| f.readlines}
