Foreach loop that skips the header line of a file (csh)


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Foreach loop that skips the header line of a file (csh)
# 1  
Old 10-12-2011
Foreach loop that skips the header line of a file (csh)

Hello all,

I'm working on a foreach loop to compare a couple sets of data. However, each datafile includes a header row. I'm wondering if it is possible to tell the foreach loop to skip the first line of data.

I've been using the basic code as follows:

foreach line ("`file.csv`")
set some variables
do stuff
end

I suppose I could probably throw an if statement in there (e.g., if the first element of the line is the first element of the header, continue; else, do stuff), but I'm not really sure how to write that, either.

Thanks for your help.
# 2  
Old 10-12-2011
That's a useless use of backticks and, if you'd managed to get it to work at all, would have been a useless use of cat too. That's a ticking timebomb in a variety of ways -- if your file's bigger than 4K that loop will blow up on many systems. If your data ever might have spaces in it, it will occasionally split in places you didn't want it to. And on.

while read is safer and more efficient.

Code:
exec 5<filename # Open file into FD 5
read SKIP # Read and ignore the first line from FD 5

while read LINE
do
...
done <&5 # Read from FD 5
exec 5<&- # Close FD 5

This User Gave Thanks to Corona688 For This Post:
# 3  
Old 10-13-2011
Thanks a lot... I'll give this a shot!
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Foreach: Words not parenthesized. csh

Just started shell scripting for the first time today :D Can anyone tell me why I get the error "foreach: Words not parenthesized." for my following code? The program takes in a list of arguments. foreach card ($argv) echo Hello end (3 Replies)
Discussion started by: pkuebler
3 Replies

2. UNIX for Dummies Questions & Answers

Foreach loop through subdirectories in csh

Hi You might find it very trivial but actually don't know how to loop through all sub-directories and their child directories into a csh. bash was easier I believe but here I am, stuck with csh. So elaborately here's my problem: Let's say I have my parent directory named C-H/ under which I have... (15 Replies)
Discussion started by: saleheen
15 Replies

3. UNIX for Dummies Questions & Answers

foreach loop in csh

Hi everyone I'm new to unix and encountered a small problem i couldnt find out a reason why it doesn't work..please help.. in my csh script when i tried to use the foreach loop like this: foreach x ( ls ) echo $x end when i tried to run it, it printed out 'ls' to the std out instead of... (3 Replies)
Discussion started by: ymc1g11
3 Replies

4. Shell Programming and Scripting

How to loop(Iterate) through List with foreach(csh)

Hey all,, I know cshell is harmful:) but I am using this just "to know" - for educational purposes!... not for a long-term use. lets say i have a list.. set arr=(x y z e f) I wanna iterate the list with foreach ,, not with while.!! foreach i $arr echo $i end does not work (2 Replies)
Discussion started by: eawedat
2 Replies

5. Shell Programming and Scripting

foreach in csh

I have a foreach loop in a csh script and I noticed that it tries to find the files with the pattern *$searchpt* in the file name. I'm confused as I never specified checking for the files. foreach f ( *$searchpt* ) set fnew = `echo $f | awk -v searchpat=$searchpt \ ... (1 Reply)
Discussion started by: kristinu
1 Replies

6. Shell Programming and Scripting

Help with csh, read in a file line by line

I want to process some audio with: sox $audio1 $audio2 trim $start_time $dur How can I batch process them by read in a file containing the values for the variables above on every line, like: 1.wav 1.5 1 2.wav 2.5 2 ... I tried "foreach f (`cat file`)" but cannot read in line by line, and... (3 Replies)
Discussion started by: dustinwang2003
3 Replies

7. Shell Programming and Scripting

foreach loop

Hi everyone Does anyone know what is wrong with this script. i keep getting errors foreach filename (`cat testing1`) set string=$filename set depth=`echo "$string" echo $depth end the error is the following testing: line 1: syntax error near unexpected token `(' testing: line 1:... (3 Replies)
Discussion started by: ROOZ
3 Replies

8. Shell Programming and Scripting

foreach loop

Hi Guys, I have a loop which uses a wildcard i.e. foreach f (*) but when I execute the tcsh file in unix then it gives me an error ->>>>>>>foreach: words not parenthesized<<<<<<<<<<- Any help. (1 Reply)
Discussion started by: abch624
1 Replies

9. Shell Programming and Scripting

why read line skips some lines...

Dear Guru: This has got to be a difficult question, as I've worked on it for a good amount of time now & still puzzled... So here is the simplified logic in my code: while read LINE do #install a pkg with the name extracted from this line install_pkg name done <... (8 Replies)
Discussion started by: bluemoon1
8 Replies

10. Shell Programming and Scripting

Remove header(first line) and trailer(last line) in ANY given file

Hi, I need some help in removing the header (first line) and the trailer (last line) in a give file... The data file actually comes in EBCDIC format and I converted it into ASCII.. Now I need to strip off the first line and the last line.. I think we can use sed to do something like this:... (2 Replies)
Discussion started by: madhunk
2 Replies
Login or Register to Ask a Question