[comp.unix.questions] Variables in csh

quang@CSUFRES.CSUFRESNO.EDU (Quang Ngo) (05/02/91)

1)

In csh, we can have something like

set a = (This is a string)
echo $a[2]

which prints 'is'   

Now, let's say I have

set a = "This is a string"
               
How do I print 'str' ?


2)

Let's say I have a file 'data' which contains the following 3 lines.

John 234
Bill 123
Tom 231

and a csh script 'test'

#!/bin/csh -f
foreach a (`cat data`)
	echo $a
end

This prints

John
234
Bill
123
Tom
231

Looks like the FS (file separator) is space and/or newline, and ???
I want the FS just to be newline so that 'test' would print

John 234
Bill 123
Tom 231

The question is how?  I know how to take care these problems in (n/g)awk.

Thanks,

-Quang (quang@csufres.CSUFresno.EDU)

rickert@mp.cs.niu.edu (Neil Rickert) (05/02/91)

In article <9105020702.AA06707@csufres.CSUFresno.EDU> quang@CSUFRES.CSUFRESNO.EDU (Quang Ngo) writes:
>
>and a csh script 'test'
>
 Don't EVER have a script called test.  One of these days you will want to
write a Bourne shell script, perhaps to do the kind of things that are giving
you trouble in csh.  And as soon as you use 'test' within your sh script you
will have trouble.

-- 
=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=
  Neil W. Rickert, Computer Science               <rickert@cs.niu.edu>
  Northern Illinois Univ.
  DeKalb, IL 60115                                   +1-815-753-6940

jik@athena.mit.edu (Jonathan I. Kamens) (05/02/91)

In article <9105020702.AA06707@csufres.CSUFresno.EDU>, quang@CSUFRES.CSUFRESNO.EDU (Quang Ngo) writes:
|> 1) [how to take substrings in csh]

Csh has no facility for doing substrings.  You need to use something like awk
or expr.  Given your example:

|> Now, let's say I have
|> 
|> set a = "This is a string"
|>                
|> How do I print 'str' ?

You could do something like this:

echo "$a" | awk '{print substr($0, 11, 3)}'


|> 2) [How to keep spaces from being record separators in a data file read
|>     into a foreach loop using backquote substitution and "cat"]

Enclose the backquote substitution in double quotes.  For example, given your
data file:

|> John 234
|> Bill 123
|> Tom 231

And this script:

#!/bin/csh -f
foreach a ("`cat data`")
	echo $a
end

(which differs from yours only in the addition of backquotes), the following
output results:

John 234
Bill 123
Tom 231

-- 
Jonathan Kamens			              USnail:
MIT Project Athena				11 Ashford Terrace
jik@Athena.MIT.EDU				Allston, MA  02134
Office: 617-253-8085			      Home: 617-782-0710

jik@athena.mit.edu (Jonathan I. Kamens) (05/03/91)

In article <1991May2.165922.10558@athena.mit.edu>, I wrote:
|> (which differs from yours only in the addition of backquotes),
                                                     ^^^^^^^^^^

I should have said "double quotes."

Thanks to Robert Earl for pointing this out to me in E-mail....

-- 
Jonathan Kamens			              USnail:
MIT Project Athena				11 Ashford Terrace
jik@Athena.MIT.EDU				Allston, MA  02134
Office: 617-253-8085			      Home: 617-782-0710

itkin@mrspoc.Transact.COM (Steven M. List) (05/03/91)

quang@CSUFRES.CSUFRESNO.EDU (Quang Ngo) writes:


>1) In csh, we can have something like
>
>set a = (This is a string)
>echo $a[2]
>
>which prints 'is'   
>
>Now, let's say I have
>
>set a = "This is a string"
>               
>How do I print 'str' ?

    expr "$a" : '.\{10\}\(...\).*' 

or
    
    echo "$a" | cut -c11-13

or...

>2) Let's say I have a file 'data' which contains the following 3 lines.
>...
>The question is how?  I know how to take care these problems in (n/g)awk.

I don't have an answer to the second one in C shell syntax.  In Bourne
(/bin/sh) it's easy:

    while read $LINE
    do
        echo $LINE
    done < datafile

and variations on that theme.  But csh doesn't have anything quite like
"read".
-- 
 +----------------------------------------------------------------------------+
 :                Steven List @ Transact Software, Inc. :^>~                  :
 :           Chairman, Unify User Group of Northern California                :
 :                         itkin@Transact.COM                                 :