1


I'm trying to solve a problem using the sed command.
I have a Table with data (few rows and cols).
I want to be able to replace the string in the i,j spot with a new string.
For an example :

word1 word2 word3 word4
word5 word6 word7 word8
word9 word10 word11 word12

with the input of 1,1 and abc should return

word1 word2 word3 word4
word5 abc word7 word8
word9 word10 word11 word12

And if possible, print it to a new file.
Thanks

5
  • Does it really have to be with sed? Commented Dec 21, 2013 at 10:54
  • In addition to what @JohnZwinck mentioned, also add a correct example. Your example output with input of 1,1 and abc doesn't seem to make much sense. Commented Dec 21, 2013 at 10:55
  • 1
    @devnull Yes it does - he's counting from zero. Commented Dec 21, 2013 at 10:56
  • @AdamSpiers It somehow seemed to me that it was indexed from 0 in one direction and from 1 in other. Commented Dec 21, 2013 at 11:00
  • Yes, I'm counting from 0,0.. Commented Dec 21, 2013 at 11:37

1 Answer 1

5

Using awk might be easier:

awk -v c=1 -v r=1 -v w='abc' 'NR==r+1{$(c+1)=w}1' file
word1 word2 word3 word4
word5 abc word7 word8
word9 word10 word11 word12
Sign up to request clarification or add additional context in comments.

6 Comments

That's what I was going to suggest too. sed is a stream editor, not a record editor.
Hey,we are not allowed to use the awk command. Im not really familiar with the sed command, thats why I asked here. If you have other suggestions, I would appericiate them
Its for our homework.. :\ They specified what we should and shouldnt use.
Well good luck with sed (not impossible but will be fairly complicated in sed)
So basically you want someone to do your homework for you. You won't learn anything that way. Why don't you try a few things first and then ask when you get stuck?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.