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