Hi, I've been trying to figure this one out and found a post about this on the forum here but the solution didn't seem to work for me. Basically what I have is a file that looks something like:
stuff
morestuff
0
otherthing
0
etc
I want to substitute for the 0 but what I want to substitute will be different for each one. Any ideas how to substitute 1 at a time with sed? Keep in mind that 0 is just the value of the variable I'll be using. I've been trying something like this with no luck...
sed might not be the best tool here. However, GNU sed might work for you. Put all your substitutions, one line each, in "file2". And run sed on file1. Note, the newline in the middle of the sed script is necessary:
I have a gut feeling that the OP cannot predict what rows will be 'missing'. So gut is telling me the OP wants to derive the value of the missing row from the previous row value. In which case we'll have to know if the sample file is representative of the 'real life' data and what (if any) assumptions can be made regarding the data itself.
Hi, so thanks for the input everyone. To clarify, in my input file the 0 is just an example of the data I want to replace, this can be text or other numbers or a combination of both. I have a loop running to replace everything so basically I will have $var which is the string to look for and $replace which is the replacement for it. Since it's in a loop the values of each variable will be changed with a new iteration. The issue I had in my script originally was that I was using sed and that replaced later values of $var when it shouldn't be so I wanted to limit it to just the first instance since my script will read the file top to bottom.
In any case, it sounds like sed isn't the best method. I tried the awk from above replacing what I thought to be my variables but no luck. I tried the following: