LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Getting Range of Words from Arg List (https://www.linuxquestions.org/questions/programming-9/getting-range-of-words-from-arg-list-489754/)

tonyfreeman 10-05-2006 02:51 PM

Getting Range of Words from Arg List
 
I need to grab the name of a city from an argument list. The argument list can be several words long and would look something like this:

Code:

Cheery Creek Dam 2.7 ESE, CO
Byers 0.1 N, CO
Commerce City 9 ENE, CO

If I should pass each one of the above to my function, I would like to store the city name (i.e., "Cheery Creek Dam" or "Byers" or "Commerce City") into a variable for use later.

This is what I have so far:

Code:

#!/bin/bash

function test ()
{
  WC=$(echo "$*" | wc -w)
  COUNT=$(echo "$WC - 3" | bc)
  CITY=${1-COUNT}
  echo "$*"
  echo "WC ..... $WC"
  echo "COUNT .. $COUNT"
  echo "CITY ... $CITY"
}

test Cheery Creek Dam 2.7 ESE, CO

Result:

Code:

Cheery Creek Dam 2.7 ESE, CO
WC ..... 6
COUNT .. 3
CITY ... Cherry

How can I store "Cheery Creek Dam" into the CITY variable?

-- Tony

pete1234 10-05-2006 03:21 PM

This seems to work:

Code:

CITY=`echo $* | grep -E -o '(\b[A-Za-z]+\b.)+' | head -n 1`

tonyfreeman 10-05-2006 03:33 PM

Wow ... Thanks for the quick reply! And it does work for me :-)

pete1234 10-05-2006 03:35 PM

You're welcome.


All times are GMT -5. The time now is 11:55 PM.