|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Generate data entry and reporting .NET Web apps in minutes, straight from your database. Read our FREE whitepaper “Build Web 2.0 Applications Without Hand-Coding” Download now! |
|
#1
|
|||
|
|||
|
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
|
|||
|
|||
|
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' |
|
#3
|
|||
|
|||
|
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. |
|
#4
|
|||
|
|||
|
Try:
sed 's/|'${BIN}'|.*/|'${BIN}'|'${SIZE}'|/' |
|
#5
|
|||
|
|||
|
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}']/' |
|
#6
|
|||
|
|||
|
grep ${BIN} bin.dump | sed 's/|'${BIN}'|.*]/|'${BIN}'|'${SIZE}']/' > somefile
|
|
#7
|
|||
|
|||
|
Sorry I should have mentioned it before. I have already tried that thing and it doesn't work, nothing gets into the file.
|
|
#8
|
|||
|
|||
|
Beats me then. That shoulda worked.
|
|
#9
|
|||
|
|||
|
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. |
|
#10
|
|||
|
|||
|
It worked... Thanx mate that was a greate help, really appreciate it and now I know why the > didn't work too...
|
![]() |
| Viewing: Dev Shed Forums > Operating Systems > UNIX Help > Sed command in Unix |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|