Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!wuarchive!mit-eddie!bloom-beacon!eru!hagbard!sunic!dkuug!freja.diku.dk!kimcm From: kimcm@diku.dk (Kim Christian Madsen) Newsgroups: comp.unix.questions Subject: Re: How read a line of a file from C-shell? Message-ID: <1990Oct31.232525.7990@diku.dk> Date: 31 Oct 90 23:25:25 GMT References: <8900@ncar.ucar.edu> <4339@aix.aix.kingston.ibm.com> Organization: Department Of Computer Science, University Of Copenhagen Lines: 74 nwosuck@aix.aix.kingston.ibm.com (Kingsley Nwosu) writes: ->In article <8900@ncar.ucar.edu>, tparker@bierstadt.scd.ucar.edu (Tom Parker) writes: ->> ->> ->> I tried something like foreach line(`cat file`) ->> ->> The only method I've gotten to work is this inelegant structure: ->> ->> set line = `head -$n | tail -1` # Read n-th line ->> ->> Does anyone have better ways to do file I/O in a C-shell script? ->> ->On my IBM AIX/370 OS I am able to do: ->cat | awk '{FS=CR; print $1}' ->This prints each line. You can then pipe the output to the desired function, ->if that is what you want. If your system is running system V use the command line(1) otherwise you might want to implement the following code: ----------line.c-----Public Domain Version--------------- #include main() { char line[BUFSIZ]; fgets(line,BUFSIZ,stdin); fputs(line,stdout); } ---------------------------------------------------------- For more flexibility you can use the following program as well: --------gets.c------Public Domain Version----------------- /* * gets.c: * Read a line from input and return the read data, if empty return * the default value given as arguments. No interpretation of the * arguments is performed, however it is allowable not to specify * any arguments at all, in which case nothing will be returned. * * (c) Copyright 1988, Kim Chr. Madsen * All Rights Reserved */ #include static char *sccsid = "@(#)gets.c 1.1 90/11/01 00:22:43"; main(argc,argv) int argc; char *argv[]; { char buf[BUFSIZ]; int def; gets(buf); if (strlen(buf)) printf("%s\n",buf); else { for (def=1; def