Re: Unix script question (basic)

From: those who know me have no need of my name (not-a-real-address_at_usa.net)
Date: 03/06/04

  • Next message: those who know me have no need of my name: "Re: korn shell script ???"
    Date: 06 Mar 2004 00:08:18 GMT
    
    

    in comp.unix.questions i read:

    >Some time ago on these boards someone suggested a good
    >way of finding out how many columns there were in a file

    this is a newsgroup, not a `board'.

    >read x < $1
    >set -- $x
    >columns=$#
    >
    >I have two questions
    >
    >1) what effect is set -- $x having (ie in enabling subsequently for $# to
    >have meaning)

    it performs word splitting and assigns the results to the positional
    parameters, this should be explained in your shell's documentation.

    >2) if we were to put this into a loop to read every single row in the file
    >and output the number of columns, can we use a construct like <command> |
    >while read row
    >
    >if so what can we have for 'command'? cat looks at a field at a time

    cat doesn't `look' at anything. if you want to run a command in a loop and
    have each iteration obtain subsequent data from a file then the easiest
    method is to redirect stdin for the loop, not for any individual command
    within the loop, e.g.,

      while read x; do set -- $x; echo "columns: $#"; done < "file"

    -- 
    a signature
    

  • Next message: those who know me have no need of my name: "Re: korn shell script ???"

    Relevant Pages

    • Re: For Loop and Spaces
      ... The "for i in `cat $` is expanding the file like this: ... so the "for" command reads in each section separated by the spaces. ... it is still a bad idea to use the for loop like this. ...
      (comp.unix.shell)
    • Re: Error when Reusing ADO Connection & Command
      ... it fails on the execute command within the body of the loop. ... How should I re-use the connection, command, recordset? ... Please reply to the newsgroup. ...
      (microsoft.public.data.ado)
    • Re: Error when Reusing ADO Connection & Command
      ... it fails on the execute command within the body of the loop. ... How should I re-use the connection, command, recordset? ... Please reply to the newsgroup. ...
      (microsoft.public.data.ado)
    • bourne shell questions
      ... Here is a simple question on managing space inside lines of a file ... which I would like to use as is in a command ... I also tried with a "for" loop, ... for i in `cat toto.txt`; do ls $i; done ...
      (comp.unix.shell)
    • Unix script question (basic)
      ... if we were to put this into a loop to read every single row in the file and ... if so what can we have for 'command'? ... cat looks at a field at a time ...
      (comp.unix.questions)