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.

% 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.

maybe you must loop on sample.txt and affect each line in a variable which can be used later, like this :

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

Hi.

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:

#!/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:

% ./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

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.

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.

#!/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:

 
 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.

Hi.

What would you want it to do with a blank line? ... cheers, drl

1 Like

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

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

I put it into a variable

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:

a b c
e
 
f

Do you have any idea?

Best Regards,

Hi.
The additional code in the final while loop seems to work when the empty lines are substituted earlier with an arbitrary (but known) character, for example, #, see the sed line:

#!/usr/bin/env tcsh

# @(#) s2	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

sed -i 's/^$/#/' data1

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]|, before test and change."
if ( "$y[$i]" =~ "#*" ) set y[$i] = ""
echo " item $i is |$y[$i]|, after  test and change."
@ i++
end
echo " The || symbols may be removed, they are there for emphasis."

exit 0

producing:

% ./s2

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 |#|
 y is |a b c e # f|

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

 item 1 is |a b c|, before test and change.
 item 1 is |a b c|, after  test and change.
 item 2 is |e|, before test and change.
 item 2 is |e|, after  test and change.
 item 3 is |#|, before test and change.
 item 3 is ||, after  test and change.
 item 4 is |f|, before test and change.
 item 4 is |f|, after  test and change.
 The || symbols may be removed, they are there for emphasis.

See man tcsh for details ... cheers, drl

Reminder:

Hi drl

Thank you for your reply.

I have abstracted my problem by using the txt file as input source. In fact, it might be output of other commands.
For example

 
set x = ( "`isql -Uu -Pp -Ss`" )

So, I can't change contents of the input.

Anyway , I felt the limit of CSH and will try perl to solve my problem.

Thank you for your patience and time.