#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Posts
    9
    Rep Power
    0

    Sed command in Unix


    I want to get a bin number from one file and change the bin details of the bin in a different file. I wrote the following script but it doesn't seem to work.
    #!/bin/sh

    i=1

    cat bin_size_changes.txt | while read LINE
    do
    BIN=`echo ${LINE} | awk '{print $1}'`
    SIZE=`echo ${LINE} | awk '{print $3}'`
    grep ${BIN} bin.dump | sed -n
    's/"|${BIN}|[A-Z]*|"/"|${BIN}|${SIZE}|"/'
    echo $LINE $i
    echo $BIN
    i=`expr $i + 1`
    done

    I believe there is somethign wrong in the sed command. Can any one help me with this pls.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2003
    Posts
    121
    Rep Power
    11
    I have no idea of what your data looks like. Nor what you are trying to do. I don't even know what the heck a "bin" number is supposed to be.

    Your sed command looks pretty bad. It's not clear what effect you're trying achieve, so I can't tell you how to fix it. But here are 2 comments that may help.

    sed -n means don't print anything unless explicitly told to. You're not explicitly telling sed to print anything, so it won't. One way tio do that is to use a p flag on your substitute command. So
    sed -n 's/cat/dog/p'
    will change cat to dog then print the result.

    Next, you can't put shell variables inside single quotes.
    THIS=cat
    THAT=dog
    sed -n 's/${THIS}/${THAT}/p'
    won't work. You could use double quotes. Or you could break the singles to expose the variables:
    sed -n 's/'${THIS}'/'${THAT}'/p'
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Posts
    9
    Rep Power
    0
    I tried your code but it doesn't seem to work though. The data in the file is set out like this

    |234|0||23 Jun||BIN NUMBER|SIZE OF BIN|

    I have a set of bin numbers in a file. All I want to do is to get the line of data containing the Bin number and change the size to a different size. I would be glad if you could help me in this regard.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2003
    Posts
    121
    Rep Power
    11
    Try:

    sed 's/|'${BIN}'|.*/|'${BIN}'|'${SIZE}'|/'
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Posts
    9
    Rep Power
    0
    Thanx mate it worked. Infact i had the pattern wrong too. appreciate the help but can you pls tell me how i can put the output of this script into a file. just the out put of the following sed command.
    grep ${BIN} bin.dump | sed 's/|'${BIN}'|.*]/|'${BIN}'|'${SIZE}']/'
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2003
    Posts
    121
    Rep Power
    11
    grep ${BIN} bin.dump | sed 's/|'${BIN}'|.*]/|'${BIN}'|'${SIZE}']/' > somefile
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Posts
    9
    Rep Power
    0

    Unhappy


    Sorry I should have mentioned it before. I have already tried that thing and it doesn't work, nothing gets into the file.
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2003
    Posts
    121
    Rep Power
    11
    Beats me then. That shoulda worked.
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2003
    Posts
    121
    Rep Power
    11

    Upon further review...


    I have an idea. I read your last question too literally. You are invoking that command in a loop. Each iteration of the loop redefines the contents of the output file. That's true even if there are no lines found by the grep.

    Try using:
    >> somefile

    This way each iteration adds to the file.
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Posts
    9
    Rep Power
    0

    Thumbs up


    It worked... Thanx mate that was a greate help, really appreciate it and now I know why the > didn't work too...

IMN logo majestic logo threadwatch logo seochat tools logo