how to take 1st word in string

how to take 1st word in string

Post by Matthew Kel » Sun, 02 Jul 1995 04:00:00



Hello... I'm trying to write a HTML form that allows our users that are
locked into a Lynx (web browser) interface to lookup email aliases...
I have a cgi-bin that parses the forms input into the cgi-bin script into
a variable, $query.  It works fine if they only enter one user-id, but if
they enter more than one ID on the line, the following gawk command
messes up.. So, as I'm trying to idiot-proof the form, I have the command

query=`echo "$query" | gawk '{print $1}'`

which I intended to grab the first word from the string $query and dump
the rest -- but for some reason it doesn't work.. This is running in a
#!/bin/sh script.. (I've tried the same command from the commandline and
it works fine!)

Any help would be appreciated.. Please cc: it to

-----------------------------------------------------------------------------
Matt Kelly

 
 
 

how to take 1st word in string

Post by Gora Mohan » Tue, 04 Jul 1995 04:00:00



Quote:>query=`echo "$query" | gawk '{print $1}'`

I can't see anything wrong in this. It works fine for me on an ULTRIX box
(where sh is actually sh5), viz.,

$ query="you me"
$ query=`echo "$query" | gawk '{print $1}'`
$ echo $query
you

Isn't that what you wanted? Could there be something wrong in how you
initially get $query? Do you check to see that it is indeed set?
                                   Regards,
                                       Gora

 
 
 

how to take 1st word in string

Post by Michael Campbe » Tue, 04 Jul 1995 04:00:00





>>query=`echo "$query" | gawk '{print $1}'`

>I can't see anything wrong in this. It works fine for me on an ULTRIX box
>(where sh is actually sh5), viz.,

>$ query="you me"
>$ query=`echo "$query" | gawk '{print $1}'`
>$ echo $query
>you

>Isn't that what you wanted? Could there be something wrong in how you
>initially get $query? Do you check to see that it is indeed set?  Regards,
>Gora

Another way which doesn't spawn a *awk process:

$ query="you me"
$ set - `echo $query`
$ echo $1
you
$ echo $2
me
$ echo $*
you me
$ echo $#
2

 
 
 

how to take 1st word in string

Post by rossb.. » Wed, 05 Jul 1995 04:00:00



Mohanty) writes:

> (Matthew Kelly) writes:

> >query=`echo "$query" | gawk '{print $1}'`

> I can't see anything wrong in this. It works fine for me on an ULTRIX box
> (where sh is actually sh5), viz.,

Save a process per day:

expr "$query" : '\([^ ]*\)'

might be a bit more effective than starting awk and opening a pipe.

Barny :-{)

--

 
 
 

how to take 1st word in string

Post by David W. Tamk » Wed, 05 Jul 1995 04:00:00



| expr "$query" : '\([^ ]*\)'
|
| might be a bit more effective than starting awk and opening a pipe.

Small problem: if $query begins with a space, you'll get nullity out of
expr instead of the first contiguous non-blank string within $query, since
expr automatically left-anchors its substring searches.  And you haven't
begun to deal with the possibility of tabs.

Maybe this:

  expr "$query" : '[  ]*\([^  ]*\)'

The first pair of brackets enclose a space and a tab; the second pair enclose
a caret, a space, and a tab.

Has anyone suggested this (assuming we're in a csh-free zone)?

  firstword=`(set -- $query ; echo $1)`

 
 
 

how to take 1st word in string

Post by Dan Merc » Thu, 06 Jul 1995 04:00:00


: Hello... I'm trying to write a HTML form that allows our users that are
: locked into a Lynx (web browser) interface to lookup email aliases...
: I have a cgi-bin that parses the forms input into the cgi-bin script into
: a variable, $query.  It works fine if they only enter one user-id, but if
: they enter more than one ID on the line, the following gawk command
: messes up.. So, as I'm trying to idiot-proof the form, I have the command

: query=`echo "$query" | gawk '{print $1}'`

: which I intended to grab the first word from the string $query and dump
: the rest -- but for some reason it doesn't work.. This is running in a
: #!/bin/sh script.. (I've tried the same command from the commandline and
: it works fine!)

: Any help would be appreciated.. Please cc: it to

: -----------------------------------------------------------------------------
: Matt Kelly

How are you setting query?  If with a read,
then :

read query waste

Else:

set -- $query
query=$1

--
Dan Mercer                                            ClearSystems, Inc

======================================================================
All opinions expressed are my own and do not reflect the opinions of
my employer or my employer's clients,  in particular 3M Company.
All advice or software offered or presented is provided As Is with no
warranty either expressed or implied.  Follow at your own risk.
Objects in the mirror are closer than they appear.

 
 
 

how to take 1st word in string

Post by Matthew Kel » Thu, 06 Jul 1995 04:00:00


I'd like to thank everyone for their help with this one... It seems that
the $query variable had some sort of control characters in it that threw
things off...

Anyways, the problem is now solved, and thanks for the tips!
----------------------------------------------------------------------------
Matt Kelly

 
 
 

1. how to remove redunt word from space seperated word string

Hi,
  The string I have is space seperated word string like " a a b b b c",
I want to trim it so that each word only shows up once, "a b c".

  uniq command deals with lines, so I run sed to replace the space with
\n, and then run uniq. Here is my command,
      echo $my_string | sed -e "s/ /\n/g" | uniq > tmp.file

The problem is how to restore the newline seperated string into space
seperated. sed does not recognize \n as search string.

  Or any other way to do this?

2. looking for driver for "TURBOWAYS 25 ATM PCI Adapter"

3. sorting words in string by length of word

4. Win2000 and linux server unified login?

5. convert 1st char of a word to uppercase

6. Strange Network Problem

7. How to convert 1st letter of string to uppercase ??

8. Linux and HP660C InkJet?

9. bash: remove 1st part of a string

10. grabbing 1st 5 char of a string

11. only 1st letter of string getting tr'd

12. Taking a Microsoft Word upload and converting it to text

13. LILO not taking dual nic string