Page 1 of 3 123 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2010
    Posts
    26
    Rep Power
    0

    Ksh loop problem


    hi all,
    i got 2 file:

    config.txt
    path1 4 group1
    path2 5 group2
    path3 6 group3


    Code:
    #!/bin/ksh
    folder=" /ebsxe/ebs25/users/mmlim/assignment01/dir01 "
    cd $folder
    
    while read file
    do
    
       set $(echo ${file})
    
              cd $1
    
              var=`ls -1 | wc -l`
    
              if [[ $var -lt $2 || $var -eq $2 ]]; then
                    echo " $3"
              else
                    echo " $3 "
              fi
    
    done <config.txt
    The output for loop that statement is :
    group1
    group2
    group1

    This is my 2file:
    config_2.txt
    group1 email1
    group2 email2

    So,i want both file link together :
    output be:
    group1=group1 email1
    group2=group2 email2

    anybody can help?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2006
    Posts
    2,632
    Rep Power
    1811
    I am not convinced that your output is as you say, judged purely from that script. The reason I say so is that you start off by changing directory to the base directory specified in the script. Then with each iteration of the loop you execute another change directory command, so unless that cd (the cd $1) is failing you are dropping down a directory level with each loop. That would mean your directory structure would need to be:

    /ebsxe/ebs25/users/mmlim/assignment01/dir01/path1/path2/path3

    for it to work. Also the final line of what you quote as being the output is a path1 whereas your input file would suggest it shoudl be path3.

    Anyway, back to the isue at hand - yes you can do what yu want, quite easily. All you need do is a 'lookup' of the group in your second file using grep.
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc
  4. #3
  5. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Sep 2006
    Posts
    867
    Rep Power
    391

    Join it.


    Assuming the file out1.txt contains "output for loop" you oposeted, then try this:
    Code:
    $ join -j 1 -e '=' -o 2.1 2.2 1.1 1.2 config_2.txt out1.txt 
    group1 = group1 email1
    group2 = group2 email2
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2010
    Posts
    26
    Rep Power
    0
    Hi SimonJM,
    ya...right .that script is exactly like what u say .
    I have to read through 1 buy 1. In my script i need to read inside path1 then calculate number of file, then determine either is over the limit be set then just determine in which group located. After that just read through file 2 and determine each group email address.
    What u mean by use grep ?can u gv mean example?
    if me use grep?how come i determine group1 is under email1?


    hi LKBrwn_DBA,
    i not nderstand wht u mean..that code cannot use in my korn shell..
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2006
    Posts
    2,632
    Rep Power
    1811
    Where you have your echo " $3" and echo " $3 " commands you just need to replace them with something that outputs what you want. That is made harder as you are constantly changing directory which means you will need to hard-code the base path or - much simpler - change the content of the variable that contains that directory path:

    Code:
    #!/bin/ksh
    folder="/ebsxe/ebs25/users/mmlim/assignment01/dir01"
    cd $folder
    
    while read file
    do
       set $(echo ${file})
       cd $1
       var=$(ls -1 | wc -l)
       info=$(grep "^$3" $folder/config_2.txt)
       if [[ $var -lt $2 || $var -eq $2 ]]
       then
          echo " $3=$info"
       else
          echo " $3=$info "
       fi
    done <config.txt
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2010
    Posts
    26
    Rep Power
    0
    Hi simon,
    this code :
    Code:
    info=$(grep "^$3" $folder/config_2.txt)
    However i cannot get the output of my email part.
    It should be like this:

    group1(config.txt)==group1(config_2.txt)
    echo " $2( email from file config_2.txt) "
  12. #7
  13. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Sep 2006
    Posts
    867
    Rep Power
    391

    BS, Yes it can...


    Originally Posted by Alexandro
    hi LKBrwn_DBA,
    i not nderstand wht u mean..that code cannot use in my korn shell..
    Yes it can:
    Code:
    $ ps
       PID TTY         TIME CMD
      8156 pts/1       0:00 ksh
      8251 pts/1       0:00 ps
    
    $ cat config.txt
    path1 4 group1
    path2 5 group2
    path3 6 group3
    
    $ cat config_2.txt
    group1 email1
    group2 email2
    
    $ join -1 1 -2 3 -e '=' -o 2.3 2.0 1.1 1.2 config_2.txt config.txt
    group1 = group1 email1
    group2 = group2 email2
    
    $
    Last edited by LKBrwn_DBA; September 24th, 2010 at 02:01 PM. Reason: Corrected first file
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2006
    Posts
    2,632
    Rep Power
    1811
    Originally Posted by Alexandro
    Hi simon,
    this code :
    Code:
    info=$(grep "^$3" $folder/config_2.txt)
    However i cannot get the output of my email part.
    It should be like this:

    group1(config.txt)==group1(config_2.txt)
    echo " $2( email from file config_2.txt) "
    We may be having terminiology issues here!

    Where you said:
    So,i want both file link together :
    output be:
    group1=group1 email1
    group2=group2 email2
    I read those to be literals, but I now suspect what you meant by "group1 email1" is just the email allied to group1, thus we need to parse the returned information a little to get it to just comeback with the 2nd column of your second file.
    A simple change:
    Code:
    info=$(grep "^$3" $folder/config_2.txt| awk 'print $2')
    should do the job. If I am misunderstanding please let me know.
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2010
    Posts
    26
    Rep Power
    0
    hi SimonJM,
    This is the output that i want
    Code:
    if [[ $var -lt $2 || $var -eq $2 ]] then
          if [[" $3==$info" ]] then     # if [[ group1==group1]]   or  if [[ group2==group2]]
            echo " $email "             # output : email1       or    output :email2
          end
    else
         if [[" $3==$info" ]] then     # if [[ group1==group1]]   or  if [[ group2==group2]]
            echo " $email "             # output : email1       or    output :email2
          end
       fi
    However ur code :
    Code:
    info=$(grep "^$3" $folder/config_2.txt| awk 'print $2')
    The output is:
    email1
    email2
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2006
    Posts
    2,632
    Rep Power
    1811
    You have mistunderstood something (as have I!).

    Going back to your first post
    Originally Posted by Alexandro
    So,i want both file link together :
    output be:
    group1=group1 email1
    group2=group2 email2
    I assumed the output you wished to have was:
    <group name>=<email for group>
    Where <group name> is the name taken from config.txt (and referenced in the script as variable $3), <email for group> is the email that is extracted from config_2.txt and the = being a literal, to give an actual output that looks like:
    group1=group1@email.com
    I think you mistook the $3=$info part of the code as a test.
    So, what I know think you want are lines like:
    group1 group1@email.com
    If that is so, try this:
    Code:
    #!/bin/ksh
    folder="/ebsxe/ebs25/users/mmlim/assignment01/dir01"
    cd $folder
    
    while read file
    do
       set $(echo ${file})
       cd $1
       var=$(ls -1 | wc -l)
       email=$(grep "^$3" $folder/config_2.txt)
       if [[ $var -le $2 ]]
       then
          echo " $3 $email"
       else
          echo " $3 $email "
       fi
    done <config.txt
    Note I have (finally!) combined the less than or equal tests into a single, less then, or equal to make the code a little less complex.
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2010
    Posts
    26
    Rep Power
    0
    hi SimonJM,
    Code:
     email=$(grep "^$3" $folder/config_2.txt)
       if [[ $var -le $2 ]]
       then
          echo " $3 $email"
       else
          echo " $3 $email "
       fi
    The output for ur script should be :
    group1 group1@email.com
    group2 group2@email.com
    However the output that i want is:
    group1@email.com
    group2@email.com
    means set the config_2.txt like this :
    $1=group name $2=email
    group1 group1@email.com
    group2 group2@email.com
    And every time i can add or delete info at *.txt,
    not necessary to adjust the code in script
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2006
    Posts
    2,632
    Rep Power
    1811
    I am getting lost - you seem to keep changing what actual output you want. If the only data you wish to be shown is the email address then just remove the $3 from the echo,
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc
  24. #13
  25. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2010
    Posts
    26
    Rep Power
    0
    hi SimonJM,

    Sorry to made u confuse,

    Wht i mean are can read the column and row of config_2.txt
    Example in file config_2.txt ,i can add the data when i want:

    group1 jason@gmail.com
    group2 allan@gmail.com
    group3 baby@gmail.com
    group4 justin@gmail.com


    And the sricpt can read through and assign as variable $1 and $2.

    If the script find out (group name in config.txt) == (group name in config_2.txt), then the output should be :
    $2
    Example :
    group1 (group name in config.txt) == group1 (group name in config_2.txt),then the output is:
    jason@gmail.com

    group4 (group name in config.txt) == group4 (group name in config_2.txt),then the output is:
    justin@gmail.com


    But for ur code :
    Code:
    email=$(grep "^$3" $folder/config_2.txt)
       if [[ $var -le $2 ]]
       then
          echo " $email"
       else
          echo "  $email "
       fi
    The output that come out is :
    group1
    group2
    group3
    group4
  26. #14
  27. No Profile Picture
    Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2006
    Posts
    2,632
    Rep Power
    1811
    Ok, hopefully we can start to clear things up here, but we shall see!

    Given your two files (taken from first and last, so far!, posts), you have:
    config.txt
    path1 4 group1
    path2 5 group2
    path3 6 group3
    and
    config_2.txt
    group1 jason@gmail.com
    group2 allan@gmail.com
    group3 baby@gmail.com
    group4 justin@gmail.com
    You are reading config.txt, line by line, and assigning each field or column to a 'system' numeric variable by using the set command ss that for each iteration of the loop $1 becomes the pathname, $2 the number you wish to test and $3 a group name.
    For each of these lines you wish to compare the group name (in $3) with the contents of youe second file and when you find a matching entry you want the email portion of the line from the second file to be shown.

    #!/bin/ksh
    folder="/ebsxe/ebs25/users/mmlim/assignment01/dir01"
    cd $folder

    Code:
    while read file
    do
       set $(echo ${file})
       cd $1
       var=$(ls -1 | wc -l)
       email=$(grep "^$3" $folder/config_2.txt | awk '{print $2'})
       if [[ $var -le $2 ]]
       then
          echo " $email"
       else
          echo " $email "
       fi
    done <config.txt
    You could use a similar technique to assign the fields from your second file to $1, $2, etc., but that would then get confusing. In some ways it is bad enough with just the use of the awk command (and sorry, I forgot to follow through on using that in previous post!).
    If we take that line:
    Code:
    email=$(grep "^$3" $folder/config_2.txt | awk '{print $2'})
    what we are doing is assigning a value to a variable called email (which we can later refer to it's contents by using $email). The value we assign is the result of looking for the group name from the first file (in variable $3) in your second file, usign the grep command. The ^ in the command means only find at the start of the line. We then pass on the line we find with the pipe command (the | symbol) to another command (the awk) which will take the line and only output the 2nd field in it. The $2 refers ONLY to the data passed to the awk command, it has no relationship to the variable $2 being populated by the set command.
    So, given the two files above, assuming we are on the 2nd time through the loop we will have:
    path2 5 group2
    so that $1 will contain path2, $2 will have 5 and $3 will have group2. Plugging those values into the commands we get:
    Code:
    grep "^group2" $folder/config_2.txt | awk '{print $2'})
    That will find group2 at the start of a line in your second file, which will be:
    group2 allan@gmail.com
    That line will be passed on to the awk command, and from that the 2nd field (the $2) will be output and placed into the variable we call email. Thus email will be given the value of allan@gmail.com.
    So we will now have 5 variables:
    $1 - path2
    $2 - 5
    $3 - group2
    $email - allan@gmail.com
    $var - the number of directory entries

    The next bit in your code confuses me a little. You test $var for being less than or equal to $2 and then do the same thing in either case. There was a difference in your first post when there was an extra space, but that seems to have come and gone.
    In any case, you are the proud possessor of five variables, and you can do with them what you will. A simple echo " $email " will do what I think you want.
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc
  28. #15
  29. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2010
    Posts
    26
    Rep Power
    0
    hi SimonJM,
    Ya.thank for ur help i think is the output that i want.
    Can u help me 1 more time, because i be compulsary to write it C shell script.

    Can u help me to translate it to C shell script for this code :
    Code:
    email=$(grep "^$3" $folder/config_2.txt | awk '{print $2'})
    This is because in C shell script when we set the config_2.txt it would be like this:
    config_2.txt
    $ [1] = group1
    $ [2] = jason@gmail.com
    $ [3] = group2
    $ [4] = allan@gmail.com
    $ [5] = group3
    $ [6] = baby@gmail.com
    $ [7] = group4
    $ [8] = justin@gmail.com
    Compare to Korn shell ,when we set config_2.txt, it look like this:
    $ [1] = group1
    $ [2] = jason@gmail.com
    $ [1] = group2
    $ [2] = allan@gmail.com
    $ [1] = group3
    $ [2] = baby@gmail.com
    $ [1] = group4
    $ [2] = justin@gmail.com
    Can u help me solve it ?
Page 1 of 3 123 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo