<SOLVED>Grab a specific "word" from a string based on position

Forum rules
Before you post please read how to get help
Post Reply
User avatar
Fred Barclay
Level 12
Level 12
Posts: 4215
Joined: Sat Sep 13, 2014 11:12 am
Location: Swimming

<SOLVED>Grab a specific "word" from a string based on position

Post by Fred Barclay »

One of my projects (that searches for updates to Atom Beta and applies them) will consistently return a string like the following:
/atom/atom/releases/download/v1.10.0-beta7/atom-amd64.deb
It is always 6 "words" long separated by as many '/'. I want to grab the 5th "word" out of this string so I can compare the latest version of Atom Beta with my currently installed version. As releases progress, the 5th word is the only one that changes: i.e. it's always preceded by "download" and followed by "atom-amd64.deb".

So far, I've only been able to extract the 5th word by passing it through cut: | cut -c 31-42. This is a pretty crude method as the length of the word will change. For example, when it was v1.9.0-beta4 I would have only cut 31-41. I've also tried using awk but so far as I can tell it only works for columns/rows, not splitting up a single string with no white spaces.

So, I'd really love it if someone would show me how to extract the 5th "word" out of this string, regardless of its length?
Thanks!
Last edited by Fred Barclay on Mon Aug 29, 2016 10:57 pm, edited 1 time in total.
Image
"Once you can accept the universe as matter expanding into nothing that is something, wearing stripes with plaid comes easy."
- Albert Einstein

User avatar
slipstick
Level 5
Level 5
Posts: 955
Joined: Sun Oct 21, 2012 9:56 pm
Location: Somewhere on the /LL0 scale

Re: Grab a specific "word" from a string based on position

Post by slipstick »

Have you tried using cut with a specified field number and field delimiter, something like:

Code: Select all

cut -d '/' -f 5
?

There's some more detail about this in the "Linux Command Line Book" available online here:
http://linuxcommand.org/tlcl.php

Chapter 20, about page 283.

EDIT: Looks like you need to use -f 6 for the fifth field - I think the initial / makes the first word "atom" to be interpreted as field 2 - I suppose field 1 is a null?
In theory, theory and practice are the same. In practice, they ain't.

User avatar
WharfRat
Level 21
Level 21
Posts: 13740
Joined: Thu Apr 07, 2011 8:15 pm

Re: Grab a specific "word" from a string based on position

Post by WharfRat »

The cut -d '/' -f 6 should do you fine:

Code: Select all

[bill@rosa] ~ $ echo "/atom/atom/releases/download/v1.10.0-beta7/atom-amd64.deb"|cut -d '/' -f 6
v1.10.0-beta7
[bill@rosa] ~ $ echo "/atom/atom/releases/download/v1.10.0-beta707/atom-amd64.deb"|cut -d '/' -f 6
v1.10.0-beta707
[bill@rosa] ~ $ echo "/atom/atom/releases/download/new-v1.10.0-beta707/atom-amd64.deb"|cut -d '/' -f 6
new-v1.10.0-beta707
[bill@rosa] ~ $ echo "/atom/atom/releases/download/another-new-v1.10.0-beta707/atom-amd64.deb"|cut -d '/' -f 6
another-new-v1.10.0-beta707
[bill@rosa] ~ $ 
ImageImage

User avatar
Fred Barclay
Level 12
Level 12
Posts: 4215
Joined: Sat Sep 13, 2014 11:12 am
Location: Swimming

Re: Grab a specific "word" from a string based on position

Post by Fred Barclay »

Yep--sorry, I thought I'd already replied! :) cut -d '/' -f 6 works perfectly!
Image
"Once you can accept the universe as matter expanding into nothing that is something, wearing stripes with plaid comes easy."
- Albert Einstein

blockhead47
Level 2
Level 2
Posts: 91
Joined: Wed Jun 15, 2016 4:50 pm
Location: Chicago, IL, USA

Re: <SOLVED>Grab a specific "word" from a string based on position

Post by blockhead47 »

Just to follow-up, awk also works

Code: Select all

 | awk -F/ '{print $6}'

User avatar
Fred Barclay
Level 12
Level 12
Posts: 4215
Joined: Sat Sep 13, 2014 11:12 am
Location: Swimming

Re: <SOLVED>Grab a specific "word" from a string based on position

Post by Fred Barclay »

Thanks blockhead47. :)
Image
"Once you can accept the universe as matter expanding into nothing that is something, wearing stripes with plaid comes easy."
- Albert Einstein

Post Reply

Return to “Scripts & Bash”