Problem with sed

I need to insert, or replace a character at a designated row and column in a text file with sed. Heres the code:

sed "${row} s/./${newchar}/${col}" >test.txt

Everytime I use this code on cygwin, It doesn't work, it just has me input text without an end.

So why isn't it working?

Also, how I can put a variable in the "row", "newchar", and "col", area so it can move to a script designated spot in the text file?

can you post your thread with example that really helpful to everyone.

The example is on my first post

Your example does not use an input file

sed "${3} s/./${\n}/${10}" <test.txt >test.txt

The >test.txt shell redirection truncates test.txt before sed is even invoked. You need to use a temp file for that (even if it's implicit, such as via gnu sed's -i).

Regards,
Alister

${col} doesn't mean that it will replace in the column you have specified. For eg:-

sed '3 s/you/me/4' file

The above code means, replace "you" with "me" in line #3 for the fourth occurrence of "you" and not in fourth column.

--ahamed

Did what you said. Made a temporary file. I see nothing outputted to the file. Can you give me what you put for it to work?

can you post a sample input and expected output?

--ahamed

The input is supposed to be an empty file.

Then the script is supposed to output a text string at the 3rd line, 10th column.

I keep getting no output.

if the input file is empty, sed will fail for sure. It will not be able to find the line # in the first place.

--ahamed

Can sed output an empty file?

And like I said, there is no column concept in sed!

--ahamed

---------- Post updated at 01:32 PM ---------- Previous update was at 01:31 PM ----------

what do you mean?

--ahamed

---------- Post updated at 01:33 PM ---------- Previous update was at 01:32 PM ----------

ok, got it.

of course, sed can output an empty file even if there is no output! The magic keyword here is ">".

--ahamed

sed can't work with columns?

AFAIK, I don't think sed recognizes any column.

--ahamed

What about grep, or awk? Is there anything I can do?

you need to add something in the 3rd row 10th column? How do you separate the column? space?
And do you have a pattern? I mean is it always at the 3rd row?

--ahamed

---------- Post updated at 02:04 PM ---------- Previous update was at 01:45 PM ----------

I really don't know what you are trying to accomplish. The following code will add blank lines and then insert the value at the specified row.

val=123
row=5
awk -v row=$row -v file=file ' BEGIN{cmd="wc -l " file;cmd|getline;new_line=row-$1; while(new_line>0){print "" >> file;new_line--;}} ' file 
&& sed -i "$row i $val" file

--ahamed

Well the idea is that im creating a paint program. So the file starts as a canvas of black escape codes. So here is what the input file would look like:

\E[0;40m \E[0;40m \E[0;40m \E[0;40m \E[0;40m \E[0;40m \E[0;40m
\E[0;40m \E[0;40m \E[0;40m \E[0;40m \E[0;40m \E[0;40m \E[0;40m
\E[0;40m \E[0;40m \E[0;40m \E[0;40m \E[0;40m \E[0;40m \E[0;40m
\E[0;40m \E[0;40m \E[0;40m \E[0;40m \E[0;40m \E[0;40m \E[0;40m
\E[0;40m \E[0;40m \E[0;40m \E[0;40m \E[0;40m \E[0;40m \E[0;40m
\E[0;40m \E[0;40m \E[0;40m \E[0;40m \E[0;40m \E[0;40m \E[0;40m
\E[0;40m \E[0;40m \E[0;40m \E[0;40m \E[0;40m \E[0;40m \E[0;40m

So the idea is that, when the user paints a different colored pixel, one of the escape codes on the canvas have to change.

So how can I use sed to go to a certain line, and move to a certain column to replace one of the black escape codes like for example, replace with a blue escape code.

Here is the desired output. I want to save the blue escape code on the 3rd line of the canvas, On the 5th column of escape codes. Like so:

\E[0;40m \E[0;40m \E[0;40m \E[0;40m \E[0;40m \E[0;40m \E[0;40m
\E[0;40m \E[0;40m \E[0;40m \E[0;40m \E[0;40m \E[0;40m \E[0;40m
\E[0;40m \E[0;40m \E[0;40m \E[0;40m \E[0;45m \E[0;40m \E[0;40m
\E[0;40m \E[0;40m \E[0;40m \E[0;40m \E[0;40m \E[0;40m \E[0;40m
\E[0;40m \E[0;40m \E[0;40m \E[0;40m \E[0;40m \E[0;40m \E[0;40m
\E[0;40m \E[0;40m \E[0;40m \E[0;40m \E[0;40m \E[0;40m \E[0;40m
\E[0;40m \E[0;40m \E[0;40m \E[0;40m \E[0;40m \E[0;40m \E[0;40m

The sed statement itself is correct. It just needs an input file:

sed "${row} s/./${newchar}/${col}" infile > test.txt

and the input file would have to contain the rows and column values..

Something like this?

awk -v row=3 -v col=5 -v val='\\E[0;45m' '{if(NR==row){$col=val}}1' input_file > new_file

--ahamed