LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 10-05-2006, 02:51 PM   #1
tonyfreeman
Member
 
Registered: Sep 2003
Location: Fort worth, TX
Distribution: Debian testing 64bit at home, EL5 32/64bit at work.
Posts: 187

Rep: Reputation: 30
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
 
Old 10-05-2006, 03:21 PM   #2
pete1234
Member
 
Registered: May 2005
Distribution: Slack, FreeBSD,NetBSD, OpenBSD, Open Solaris, Minix
Posts: 172

Rep: Reputation: 30
This seems to work:

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

Last edited by pete1234; 10-05-2006 at 03:23 PM.
 
Old 10-05-2006, 03:33 PM   #3
tonyfreeman
Member
 
Registered: Sep 2003
Location: Fort worth, TX
Distribution: Debian testing 64bit at home, EL5 32/64bit at work.
Posts: 187

Original Poster
Rep: Reputation: 30
Wow ... Thanks for the quick reply! And it does work for me :-)
 
Old 10-05-2006, 03:35 PM   #4
pete1234
Member
 
Registered: May 2005
Distribution: Slack, FreeBSD,NetBSD, OpenBSD, Open Solaris, Minix
Posts: 172

Rep: Reputation: 30
You're welcome.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash - How to make For read lines instead of words in a list ? landuchi Programming 10 02-15-2006 01:36 PM
How to list file space usage estimate (du), SPECIFIED BY RANGE tsilok Linux - Newbie 6 10-24-2005 04:56 AM
How to List files between date1 and date2 (range of time)? mystique98ls Linux - Newbie 2 09-10-2004 04:36 PM
Sorting a list of words in LISP! Hady Programming 1 05-01-2004 03:29 PM
This might sound strange, but I need a big list of dirty words. Travis86 Programming 12 07-17-2003 11:06 PM


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

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration