# Getting the last word in each line

**URL:** <https://www.ruby-forum.com/t/getting-the-last-word-in-each-line/68474>\
**Category:** Ruby\
**Created:** [September 12, 2006, 11:58pm UTC](https://www.ruby-forum.com/t/getting-the-last-word-in-each-line/68474 "2006-09-12T23:58:09Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![joe-dumoulin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9dc877/32.png) [@joe-dumoulin](https://www.ruby-forum.com/u/joe-dumoulin)\
**Post date:** [September 12, 2006, 11:58pm UTC](https://www.ruby-forum.com/t/getting-the-last-word-in-each-line/68474/1 "2006-09-12T23:58:09Z")

</div>

Is there a prettier way to read a file and return the last word in each  
line?

Here’s my go:

File.read(some\_file).to\_a.map { |l| line.split.last }

What if I wanted to search the lines (based on a regex) and return the  
last word in each line that satisfied the search criteria?

Joe

---

<div class="post-metadata">

**Author:** ![joe-dumoulin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9dc877/32.png) [@joe-dumoulin](https://www.ruby-forum.com/u/joe-dumoulin)\
**Post date:** [September 13, 2006, 12:20am UTC](https://www.ruby-forum.com/t/getting-the-last-word-in-each-line/68474/2 "2006-09-13T00:20:10Z")

</div>

Hello !

> File.read(some\_file).to\_a.map { |l| line.split.last }
> 
> What if I wanted to search the lines (based on a regex) and return the  
> last word in each line that satisfied the search criteria?

What about

File.read(some\_file).to\_a.map { |l| line.split.select {|v| v =~  
/regex/}.last }

```
Vince
```

---

<div class="post-metadata">

**Author:** ![joe-dumoulin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9dc877/32.png) [@joe-dumoulin](https://www.ruby-forum.com/u/joe-dumoulin)\
**Post date:** [September 13, 2006, 12:24am UTC](https://www.ruby-forum.com/t/getting-the-last-word-in-each-line/68474/3 "2006-09-13T00:24:10Z")

</div>

On Wed, Sep 13, 2006, Joe Van D. wrote:

> Is there a prettier way to read a file and return the last word in each  
> line?

Well, there’s #readlines…

File.readlines(‘somefile’).map {|f| f.split.last}

…which is a little nicer.

> What if I wanted to search the lines (based on a regex) and return the  
> last word in each line that satisfied the search criteria?

File.readlines(‘somefile’).map do |f|  
next unless f =~ /regex/  
f.split.last  
end

But you’ll have nils everywhere that a line was next’ed over…

File.readlines(‘somefile’).reject{|f| f !~ /regex/}.map {|f|  
f.split.last}

Someone else might have an elegant solution, but these will work 🙂

Ben

---

<div class="post-metadata">

**Author:** ![joe-dumoulin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9dc877/32.png) [@joe-dumoulin](https://www.ruby-forum.com/u/joe-dumoulin)\
**Post date:** [September 13, 2006, 12:25am UTC](https://www.ruby-forum.com/t/getting-the-last-word-in-each-line/68474/4 "2006-09-13T00:25:17Z")

</div>

in irb:

> > r = /\w+$/  
> > =\> /\w+$/
> 
> > s = “bad boy”  
> > =\> “bad boy”
> 
> > md = s.match®  
> > =\> #MatchData:0x2cae00c
> 
> > md[0]  
> > =\> “boy”

Joe Van D. wrote:

> Is there a prettier way to read a file and return the last word in each  
> line?
> 
> Here’s my go:
> 
> File.read(some\_file).to\_a.map { |l| line.split.last }
> 
> What if I wanted to search the lines (based on a regex) and return the  
> last word in each line that satisfied the search criteria?
> 
> Joe

---

<div class="post-metadata">

**Author:** ![joe-dumoulin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9dc877/32.png) [@joe-dumoulin](https://www.ruby-forum.com/u/joe-dumoulin)\
**Post date:** [September 13, 2006, 1:24am UTC](https://www.ruby-forum.com/t/getting-the-last-word-in-each-line/68474/5 "2006-09-13T01:24:14Z")

</div>

Hi,

At Wed, 13 Sep 2006 06:57:47 +0900,  
Joe Van D. wrote in [ruby-talk:214152]:

> File.read(some\_file).to\_a.map { |l| line.split.last }

$ ruby -e ‘p open(ARGV[0]){|f|f.grep(/(\w+)\s\*$/){$1}}’ version.h  
[“190”, “20060912”, “1”, “9”, “0”, “2006”, “9”, “12”]

---

<div class="post-metadata">

**Author:** ![joe-dumoulin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9dc877/32.png) [@joe-dumoulin](https://www.ruby-forum.com/u/joe-dumoulin)\
**Post date:** [September 13, 2006, 12:34am UTC](https://www.ruby-forum.com/t/getting-the-last-word-in-each-line/68474/6 "2006-09-13T00:34:11Z")

</div>

Joe Van D. wrote:

> File.read(some\_file).to\_a.map { |l| line.split.last }

line[/(\w+)\s\*$/, 1]

might be faster than line.split.last. Worth trying anway.

---

<div class="post-metadata">

**Author:** ![joe-dumoulin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9dc877/32.png) [@joe-dumoulin](https://www.ruby-forum.com/u/joe-dumoulin)\
**Post date:** [September 13, 2006, 1:34am UTC](https://www.ruby-forum.com/t/getting-the-last-word-in-each-line/68474/7 "2006-09-13T01:34:15Z")

</div>

On Wed, Sep 13, 2006 at 07:18:44AM +0900, Vincent F. wrote:

> What about
> 
> File.read(some\_file).to\_a.map { |l| line.split.select {|v| v =~  
> /regex/}.last }  
> That call to\_a is unecessary. File.read(some\_file).map { … } will work  
> just as well.
