sed comand

Discussion in 'Mac Programming' started by rita1985, Dec 26, 2007.

  1. rita1985 macrumors newbie

    Joined:
    Dec 25, 2007
    #1
    A)
    Input
    ================
    testAAAA svc stream
    #rexd sunrpc_tcp
    #rstatd sunrpc_udp

    batch file
    ================================================
    copy D:\11\Input tmp0.txt

    sed "/#/d" tmp0.txt > Output.txt

    copy Output.txt D:\11

    del tmp*.txt

    Output
    ================
    testAAAA svc stream :)


    B) I add in ##infront of the testAAAA and add in echo No configuration found > Output.txt in the batch file.

    Input
    ================
    ##testAAAA svc stream
    #rexd sunrpc_tcp
    #rstatd sunrpc_udp

    batch file
    ================================================
    copy D:\11\Input tmp0.txt

    sed "/#/d" tmp0.txt > Output.txt

    copy output.txt D:\11

    echo No configuration found > Output.txt

    del tmp*.txt

    Output
    ================
    No configuration found :)

    But When i use the batch file from B and delete away the ##infront of testAAAA the output i get is No configuration found instead of testAAAA svc stream :(

    How should i program it in order to suit both A and B :confused:

    The result i want is when the input is all in ## my output is No configuration found. When there is a line in input that do not have # , my output will display the line. :(
     
  2. satyam90 macrumors regular

    satyam90

    Joined:
    Jul 30, 2007
    Location:
    Bangalore, India
    #2
    Am I right if I say that the result u are expecting is :
    1. When a line starts with ## it should not be deleted.
    2. When a line starts with # it should be deleted.
    3. When a line don't start with ## it should not be deleted.

    Right?
     
  3. aross99 macrumors 68000

    aross99

    Joined:
    Dec 17, 2006
    Location:
    East Lansing, MI
    #3
    You will always get "No Configuration Found", because you don't have any type of conditional statement controlling that statement.

    Your Shell will do the Copy, the Sed, the 2nd Copy, and then echo "No configuration found" - every time.

    When so you want to see this message? Maybe when there are no lines left?
     
  4. rita1985 thread starter macrumors newbie

    Joined:
    Dec 25, 2007
    #4
    1. when a line starts with ## or # it should be deleted and display no configuration found
    2. when a line don't start with ## or # it should not be deleted and it will display the line.
     
  5. rita1985 thread starter macrumors newbie

    Joined:
    Dec 25, 2007
    #5
    when there are lines it will display the lines but when there are no lines it will display no configuration found.
     
  6. satyam90 macrumors regular

    satyam90

    Joined:
    Jul 30, 2007
    Location:
    Bangalore, India
    #6
    If script can be ok for you, see the following code:

    Code:
    sed "/^#/d" test.txt > out.txt 
    if [[ `ls -lrt out.txt | awk '{print $5}'` = 0 ]] 
    then 
            echo "No Configuration found" > out.txt 
    fi
    
    Place it in a file, save it, change mode and run it.
     
  7. rita1985 thread starter macrumors newbie

    Joined:
    Dec 25, 2007
    #7
    batch file

    copy D:Input tmp0.txt

    sed "/^#/d" tmp0.txt > out.txt

    if [[ `ls -lrt out.txt | awk '{print $5}'` = 0 ]]

    then
    echo "No Configuration found" > out.txt
    fi

    copy out.txt D:\out

    del tmp*.txt

    hmm after i run the script it display `ls was unexpected at this time.

    It output 2 file in d: -> out.txt and tmp0.txt
    when there are lines in the input file, out.txt display the lines while tmp0 display the original input file.
    when all lines are in ## in input file, out.txt is blank and tmp0 display the orginal input file.
     
  8. satyam90 macrumors regular

    satyam90

    Joined:
    Jul 30, 2007
    Location:
    Bangalore, India
    #8
    I thought u might be running the script in Linux/Unix or Mac environment and the script is according to that. But I think u are running in Windows environment. "ls" command must be replaced accordingly in Windows. I will try out and let you know if I succeed.
     
  9. rita1985 thread starter macrumors newbie

    Joined:
    Dec 25, 2007
    #9
    Thanks for ur help i have solve the problem (^-^)
     

Share This Page