Find the answer to your Linux question:
Results 1 to 5 of 5
Ok look at the following file test.file Code: 1 9199=4p 121 JOHN.tmp DALLAS 09/10/07 # 1 - Create files 2 - Convert Times 3 - Calculate time 1 - Compute ...
  1. #1
    Just Joined!
    Join Date
    Apr 2007
    Posts
    12

    Ok now I almost have it, but not quiet..

    Ok look at the following file

    test.file
    Code:
    1  9199=4p
    121   JOHN.tmp   DALLAS   09/10/07
    #
         1 - Create files
         2 - Convert Times
         3 - Calculate time
              1 - Compute regular time
              2 - Compute Otime
         4 - Verify vacation
    
    
    Completed steps ______
    1  9199=4p
    121  MIKE.tmp   DALLAS  09/10/07
    #
         1 - Create files
         2 - Convert Times
        
    
    Completed steps ______
    1  9199=4p
    121   MARY.tmp   DALLAS   09/10/07
    #
         2 - Convert Times
         3 - Calculate time
              1 - Compute regular time
              2 - Compute Otime
         4 - Verify vacation
    
    
    Completed steps ______
    Ok I need to pull portions of this file out and save them as variables in a script. (CSH)
    I want to be able to pull the name ($name) location ($location) and date ($today). Then store the steps for each person in a variable to be displayed later.

    Here is an example of the desired output.


    Code:
    09/10/07
    ---------------------------------------------------------------------------
    Dallas  |  1  |  2  | 3.1 | 3.2 |  4  |
    
    JOHN    |  Y  |  Y  |  Y  |  Y  |  Y  |
    MIKE    |  Y  |  Y  |  -  |  -  |  -  |
    MARY    |  -  |  Y  |  Y  |  Y  |  Y  |
    I am fairly sure this can be done with "grep", "awk" and or "sed", But I am un sure of how to go about it.

    I know if I do something like
    Code:
    cat test.file |grep 121 | awk 'set $name = $2'
    (my syntax may be a bit off, but I am still learning) it will dump the John then mike then mary to the variable $name. However this does not nelp me much. Also keep inming that the name "Jonh, Mike, Mary ect..." will change on a daily basis and not necessarly be in the same order or always have the same steps everytime I run the script.

    Any input would be greatly appreciated..

    Thanks

    Carl

  2. #2
    Just Joined!
    Join Date
    Apr 2007
    Posts
    12
    Ok looking into this further all I really need to do is do something like this:

    Grep read line 1 of test.file > $thisline
    if (first 3 spaces of $thisline = 121) then
    awk $thisline '
    [print $2}' > $name
    else
    if (first 5 spaces of $thisline = " ") then
    thave what is found in 6th space to $a
    Else
    ...

    And so on.. Just not sure how to make grep read a file 1 line at a time.

    Thanks

    Carl

  3. #3
    Just Joined!
    Join Date
    Apr 2007
    Posts
    12
    Com'on anyone have an Idea here??

    This cant be THAT hard..

    It is simple How do I get my script to read a file one line at a time?
    Code:
    #! /bin/csh
    # test file name is X.FILE
    
    set thisfile = `wc -l X.FILE | awk '{print $1}'`
    set line = 1
    While ($line !> $thisfile)
      Read $line of X.FILE
          if ()then
          Else
          endif
          inc $line
      (Go back, read line 2, and repete till end of file) 
    end
    To be honist I just need help with this line:

    Code:
     Read $line of X.FILE

    I really would appreciate any help.

    Carl

  4. #4
    Just Joined!
    Join Date
    Aug 2007
    Posts
    37
    Quote Originally Posted by cdslaughter View Post
    ....How do I get my script to read a file one line at a time? ....
    If you're determined to use csh you might get more help on the BSD forums, although I think most of them are moving to bash now.
    Code:
    #! /bin/csh 
    # This works with csh and tcsh
    
    set length = `wc -l X.FILE` 
    set counter = 1 
    
    while($counter <= $length[1]) 
        set line = `head -$counter X.FILE | tail -1` 
        echo "$line"
        @ counter++ 
    end
    Code:
    #! /bin/bash 
    # This works with bash, dash, ksh, and zsh
    
    while read line; do
        echo "$line"
    done <X.FILE

  5. #5
    Just Joined!
    Join Date
    Sep 2004
    Posts
    5
    Yep, that should enable you to read the file 1 line at a time.
    You can use
    rslt=`echo $line|grep <pattern>`
    to check for each string

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...