Read text file in Cshell


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Read text file in Cshell
# 1  
Old 02-10-2009
Read text file in Cshell

I've been searching the forums for info on reading a text file in a Cshell script but nothing I'm trying is working. My latest attempt was:

set LASInputFile = `ls *.[Ll][Aa][Ss] | head -1`
echo $LASInputFile
while read line
do
echo $line
done < $LASInputFile


My error message is: while: Expression syntax

Thanks for any help

Paul Hudgens
Denver
# 2  
Old 02-10-2009
Question

Just a thought to begin this...
instead of your loop, can you replace it with a cat command?
I would just like to see if the file has anything to process
and/or if there is some kind of file error.
# 3  
Old 02-10-2009
looks like you're combining flow expression syntax for different shells....
"read" is a Bourne / Korn shell built-in... not csh.

why not just this:

Code:
set LASInputFile = `ls *.[Ll][Aa][Ss] | head -1`
echo $LASInputFile
cat $LASInputFile

# 4  
Old 02-10-2009
Thanks for the input. The cat command does display my file to the screen correctly. By the way, I'm not sure if I should reply to the user link or via Quick Reply.
# 5  
Old 02-10-2009
Code:
set LASInputFile = `ls *.[Ll][Aa][Ss] | head -1`

set i=`cat $LASInputFile | wc -l`
set j=1

while ($j <= $i)

  set line=`cat $LASInputFile | head -$j | tail -1`

  @ j = $j + 1

  # do your thing with $line here.
  # for this example, we'll just echo
  # it back the screen
 
  echo $line

end

# 6  
Old 02-11-2009
I'm getting the error message: Unknown user: VERSION

The following is the first line of my file:
~VERSION

Some of the lines in my file begin with a tilde. Is that causing a problem? Also I'm setting i=100 since what I'm after is in the top 100 lines of an otherwise very long file.

set LASInputFile = `ls *.[Ll][Aa][Ss] | head -1`
echo $LASInputFile
set j=1
while ($j <= 100)
set line=`cat $LASInputFile | head -$j | tail -1`
@ j = $j + 1
echo $line
end

Thanks for the help,

Paul H.
# 7  
Old 02-11-2009
Sorry, I've made some slight changes to your script:

Code:
set LASInputFile = `ls *.[Ll][Aa][Ss] | head -1`
echo $LASInputFile
set j=1
while ($j <= 100)
set line="`cat $LASInputFile | head -$j | tail -1`"
@ j = $j + 1
echo "$line"
end

I put quotes around the "set line" and "echo" portions of the script. This will stop the shell from evaluating ~VERSION as if it was a home directory.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Read csv file, convert the data and make one text file in UNIX shell scripting

I have input data looks like this which is a part of a csv file 7,1265,76548,"0102:04" 8,1266,76545,"0112:04" I need to make the output data should look like this and the output data will be part of text file: 7|1265000 |7654899 |A| 8|12660000 |76545999 |B| The logic behind the... (6 Replies)
Discussion started by: RJG
6 Replies

2. Shell Programming and Scripting

Read in search strings from text file, search for string in second text file and output to CSV

Hi guys, I have a text file named file1.txt that is formatted like this: 001 , ID , 20000 002 , Name , Brandon 003 , Phone_Number , 616-234-1999 004 , SSNumber , 234-23-234 005 , Model , Toyota 007 , Engine ,V8 008 , GPS , OFF and I have file2.txt formatted like this: ... (2 Replies)
Discussion started by: An0mander
2 Replies

3. Shell Programming and Scripting

Read n lines from a text files getting n from within the text file

I dont even have a sample script cause I dont know where to start from. My data lookes like this > sat#16 #data: 15 site:UNZA baseline: 205.9151 0.008 -165.2465 35.8109 40.6685 21.9148 121.1446 26.4629 -18.4976 33.8722 0.017 -165.2243 48.2201 40.6908 ... (8 Replies)
Discussion started by: malandisa
8 Replies

4. Shell Programming and Scripting

how read specific line in a file and write it in a new text file?

I have list of files in a directory 'dir'. Each file is of type HTML. I need to read each file and get the string which starts with 'http' and write them in a new text file. How can i do this shell scripting? file1.html <head> <url>http://www.google.com</url> </head> file2.html <head>... (6 Replies)
Discussion started by: vel4ever
6 Replies

5. Shell Programming and Scripting

Processing text variables that have numbers in CShell

Hello, I have a case where I need to process an environment variable text value in CShell to calculate the product of 2 numbers stated in the variable string having the format "<first_number>Letter x<second_number>". Ex: $VAR = "24x20" I need to set $VAR_PRODUCT = "Product of 20 and 24"... (3 Replies)
Discussion started by: mohy
3 Replies

6. Shell Programming and Scripting

Read tags in text file

Hello Team, I am writing a script that reads a text (say 1.txt - 2 s2 a+bb means Number State Label) file having data as: 2 s2 a+bb 3 s3 a+bb 4 s4 a+bb And there is another text file (say 2.txt) that has sample data as; ~x "a+bb" <BEGIN> <TOTAL> 3 <STATE> 1 ~y "S_2" <STATE> 2 ~y... (8 Replies)
Discussion started by: AKD
8 Replies

7. Shell Programming and Scripting

Read text from a file between two characters..

I have a requirement where i have to read from a .sh file a text lying bet characters like 'SELECT' & ';'...Please help me out in this. I am new to shell scripting. (2 Replies)
Discussion started by: goutam_igate
2 Replies

8. UNIX for Advanced & Expert Users

How can i read a non text file in unix - ELF-64 executable object file - IA64

The binary file is ELF-64 executable object file - IA64. How i know that the source is Is there any comamnd in unix i can read these kind of files or use a thirty party software? Thanks for your help (8 Replies)
Discussion started by: alexcol
8 Replies

9. Shell Programming and Scripting

Read the record from a text file

Hi I want to read one row record from a text file. For eg: I have Sample.txt file with one row of record like 123456768 I want to get the above value from the file and assign it to a variable in my script. Please guide me how to proceed. Thanks, Soll (2 Replies)
Discussion started by: sollins
2 Replies

10. Shell Programming and Scripting

help with cshell script to read 1 or more lex files

taskes one or more .l files and compiles them #!/usr/bin/csh #while loop to carry on asking user to enter the files while $number!=0 echo "enter file name" #check to see if file ends with .l #if file ends with .l compile lexx.yy.c file for each file this is how i think it needs... (1 Reply)
Discussion started by: homerj546
1 Replies
Login or Register to Ask a Question