read a file and creating variables in csh

Dell / INSPIRON 1420
March 16, 2010 at 09:20:45
Specs: Microsoft Windows Vista Édition Familiale Premium, 1.5 GHz / 1021 MB
hi,

I need to merge a few files together the problem is that I have so many files that it will takes me week to get all the job done...

In fact, I wrote a script that put in a text file the name of the files that need to be merged.

foreach stn (`cat ../stnlist`)
ls *$stn.* > tmp$stn

Now I need to add to this script a part where it read each line of tmp$stn and give each filenames (lines) a variable (first one = 1, second one = 2,...). I can't know in advance how many line I have but shouldn't be more then 10 each time.

The variables will then be put into the following script so I would also need to be able to count the numbers of lines and also put that into a variables to ask the script to merge x number of files.

/usr/local/sac/bin/sac << endsac
r $1
m $2
m $3
...
w x$1
quit
endsac

Hope someone could help me... I'm on my master thesis and I don't have weeks to do that...


See More: read a file and creating variables in csh

Report •

#1
March 18, 2010 at 09:45:34
I will leave the file reading and looping to you. This test stub builds two variables - var_1 & var_2:

#!/bin/csh

# build variable 1
set x = 1
set line="this is line 1"
set eval var_${x}="$line"
echo "var_1 is: ${var_1}"

# bump the variable counter
@ x = ($x + 1)

# build variable 2
set line="this is line 2"
set eval var_${x}="$line"
echo "var_2 is: ${var_2}"


Report •

#2
March 18, 2010 at 10:35:56
Thanks for your help. But I still have problem with the looping... as I don't know how many lines their is, I can't script for every variables.

would it be possible to do something like that:

echo "var_${x} is: ${var_${x}}"

can we put a variable into a variable's name??


Report •

#3
March 22, 2010 at 15:26:50
I still need help on that one...

Report •

Related Solutions

#4
March 22, 2010 at 18:02:26
I am not a cshell expert, but this loop reads a file a line at a time and bumps x by 1 for each line in file tmpstn:

#!/bin/csh

set x = 0
foreach line ("`cat tmpstn`")
   # increase line count by 1
   @ x = ($x + 1)
   echo "$line"
end
echo $x


Report •

Ask Question