help with multiline variable in csh


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting help with multiline variable in csh
# 1  
Old 04-08-2010
Data help with multiline variable in csh

My shell is csh and it is required.
I have a file like sample.txt
------------------------
a b c
d
e
f
g h i
------------------------
I want set the file to a variable and print it out in the same format.
I have tried something like this, but not succed.
Code:
% cat ~/tmp/sample.txt
a b c
d
e
f
g h i
% set file = `cat ~/tmp/sample.txt`
% echo $file
a b c d e f g h i
% foreach foo ( $file )
? echo $foo
? end
a
b
c
d
e
f
g
h
i

Any reply will be appreciated.
# 2  
Old 04-08-2010
maybe you must loop on sample.txt and affect each line in a variable which can be used later, like this :

Code:
cat sample.txt | while read myline
do
i=$((${i}+1))
v${i}=`echo ${myline}`
done

# 3  
Old 04-08-2010
Hi.
Quote:
Originally Posted by anykao
help with multiline variable in csh
My shell is csh and it is required.
The csh / tcsh shells do not lend themselves to do tasks which are far more easily done in the Bourne family shells.

If you must use csh, here are some ideas for you to consider:
Code:
#!/usr/bin/env tcsh

# @(#) s1	Demonstrate lines into multiple-value variable, csh

echo
setenv LC_ALL C ; setenv LANG C
echo "Environment: LC_ALL = $LC_ALL, LANG = $LANG"
echo "(Versions displayed with local utility version)"
sh -c "version >/dev/null 2>&1" && version "=o" tcsh

cat > data1 <<EOF
a b c
e
f
EOF

set x = `cat data1`
echo
echo " x is |$x|"

set x = "`cat data1`"
echo
echo " x is |$x|"

set y = ( "`cat data1`" )
echo
echo " y[1] is |$y[1]|"
echo " y[2] is |$y[2]|"
echo " y[3] is |$y[3]|"
echo " y is |$y[*]|"

echo
foreach item ( $y )
echo " item is $item"
end

echo
@ i = 1
while ( $i <= ${#y} )
echo " item $i is $y[$i]"
@ i++
end

exit 0

producing:
Code:
% ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility version)
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian GNU/Linux 5.0 
tcsh 6.14.00

 x is |a b c e f|

 x is |a b c e f|

 y[1] is |a b c|
 y[2] is |e|
 y[3] is |f|
 y is |a b c e f|

 item is a
 item is b
 item is c
 item is e
 item is f

 item 1 is a b c
 item 2 is e
 item 3 is f

See man csh for details.

However, as noted frequently in forums, you would be better off using Bourne family shells for scripting.

Good luck ... cheers, drl
# 4  
Old 04-08-2010
Thank you drl.

The code works like a charm for me.

I know csh is awkward, but my project is confined to it.

I have resolved the problem using perl.But I will give csh a try using your code.

Best regards.
# 5  
Old 05-16-2010
I have a new problem with this.

If there is a blank row in the file, the csh will skip it and output like there is no such line.

Code:
#!/bin/csh
cat > data1 <<EOF
a b c
e
 
f
EOF
 
set x = `cat data1`
echo
echo " x is |$x|"
 
set x = "`cat data1`"
echo
echo " x is |$x|"
 
set y = ( "`cat data1`" )
echo
echo " y[1] is |$y[1]|"
echo " y[2] is |$y[2]|"
echo " y[3] is |$y[3]|"
echo " y is |$y[*]|"
 
echo
foreach item ( $y )
echo " item is $item"
end
 
echo
@ i = 1
while ( $i <= ${#y} )
echo " item $i is $y[$i]"
@ i++
end
 
exit 0

producing:

Code:
 
 x is |a b c e f|
 
 x is |a b c e f|
 
 y[1] is |a b c|
 y[2] is |e|
 y[3] is |f|
 y is |a b c e f|
 
 item is a
 item is b
 item is c
 item is e
 item is f
 
 item 1 is a b c
 item 2 is e
 item 3 is f

There is no change in the result.
Does anyone have any idear?

Best Regards.
# 6  
Old 05-16-2010
Hi.

What would you want it to do with a blank line? ... cheers, drl
This User Gave Thanks to drl For This Post:
# 7  
Old 05-16-2010
Hi drl

Thank you again for your timely reply.

What I want is that the output is just the same as input.

So if I got a file
Code:
cat > data1 <<EOF
a b c
e
 
f
EOF

I put it into a variable

Code:
set x = ( "`cat data1`" )

Then I can do some business check on each row of the variable x, and output x changing nothing if possible.

So the output should be:

Code:
a b c
e
 
f

Do you have any idea?

Best Regards,
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to set variable permanent in csh?

We are using csh on our AIX platform, if we have to export/set a specific environment variable we use setenv command but its only valid till session. How do we set that variable permanent in our csh AIX? Do we put it in userprofile file or something else? (1 Reply)
Discussion started by: aixusrsys
1 Replies

2. Shell Programming and Scripting

Assign the result of a multiline command to a variable

Hi, I have the following command that lists all the .o files from all the directories except of vwin (which I don't want it) for i in `ls -d */*.o|awk '$0 !~ "vwin"'`; do echo $i; done The result is something like that dir1/file1.o dir1/file2.o dir2/file3.o etc. So, I want to create a... (9 Replies)
Discussion started by: scor6800
9 Replies

3. Shell Programming and Scripting

Csh variable calling problem

First post on here. So I use csh shells for my research (physics... not a CS person). I am trying to rerun the same scripts, but there are ~10 files that have similar variables that I have to change for each different configuration, so I would like one central file for the variables I change that... (3 Replies)
Discussion started by: sabrepride
3 Replies

4. Shell Programming and Scripting

{bash scripting} Multiline initialisation of string variable

Hello, The task is quite simple. I need to initialise а string variable and assign to it a very long value. For the reason of readability I want to devide this long value into equal parts and place each part at a separate line. For instance, I have - ... (1 Reply)
Discussion started by: Pretoria
1 Replies

5. Shell Programming and Scripting

csh and variable values with spaces

my working shell is csh and even though if I try to run my script in plain sh, it behaves the same way. Here's a simple script: #!/bin/sh desc='"test my changes"' cmd="echo \"$desc\"" $cmd I want $desc to be passed as an argument to another command, but csh apparently doesn't like spaces in... (5 Replies)
Discussion started by: iskatel
5 Replies

6. Shell Programming and Scripting

Reading a variable in csh

I have a simple script that sets a value and reads the value in csh: set -x set a = 10 echo $a The output of the script does not show the value of a + set a = 10 + echo any help would be great. (4 Replies)
Discussion started by: pt14
4 Replies

7. UNIX for Dummies Questions & Answers

address variable content with csh

By using a csh, I want to address a variable content whose name is/matches the content of a given other variable. i.e. set name=´sam´ set ${name}_age=´27´ So, by typing: echo ${name}_age I correctly obtain: sam_age By typing: echo $sam_age or echo ${sam_age} I correctly obtain: 27 ... (1 Reply)
Discussion started by: sobolev
1 Replies

8. Shell Programming and Scripting

declare number variable in csh

Hi frind, i="1" while do echo "i is $i" data_file=$HYP_ROOT/import/efcextr$i.txt echo "$data_file" i=`expr $i + 1` done This is woring finly in ksh but not in ksh. in ksh it showing error i=1: Command not found i: Undefined variable Kindly help me ...why it is showing the error... (1 Reply)
Discussion started by: deep_kol
1 Replies

9. Shell Programming and Scripting

Pass csh variable to Perl

Hi All, I am trying to put the csh variable into a perl. In the below case, i am trying to put the csh variable "var" into my perl code. I tried to use '"$var"' but i don;t think it works. Can anybody help me pls? #!/bin/csh set var = `echo "xxx"` perl myperlcode.pl file ... (9 Replies)
Discussion started by: Raynon
9 Replies

10. Shell Programming and Scripting

csh fails when setting variable

I have a csh that is called from autosys. It fails when it hits this code env | grep Rep if ( $status == 0 ) then echo "" else setenv REP "" endif However if I run it from the command line, as opposed to from autosys (job schduler) it runs fine. I thought it might be some kind of... (2 Replies)
Discussion started by: gillbates
2 Replies
Login or Register to Ask a Question