1

i was wondering weather is it possible to append data in a file without using cat command. I've considered using sed to append data , but as of my knowledge sed only operates after loading the full data into the memory. please do correct me if i'm wrong on this.

5
  • Have you tried just simple I/O redirection with >> ? >> is used for appending data rather than > which will override. Commented Jun 1, 2015 at 11:38
  • 1
    Sed does not load the full data into memory.It is a "stream editor" - hence the name "sed". Commented Jun 1, 2015 at 11:41
  • I think you need to explain why you want to do this. Commented Jun 1, 2015 at 11:46
  • well this question just struck in my mind..& couldn't found any suitable answer, so basically that's the only reason.. Commented Jun 1, 2015 at 11:48
  • Do you want to append data; or concatenate data (which is the purpose of cat). Both tasks are slightly different... Commented Jun 1, 2015 at 11:48

2 Answers 2

4

If you want to append data to a file, you can simply use the append I/O-redirection >>. For instance:

echo "first line" > file
echo "next line" >> file

Or you could append an entire file

echo "$(<otherfile)" >> file

This command is however not advisable since it will load the entire file first into memory.

A better way is to use tee:

tee < otherfile >> file
Sign up to request clarification or add additional context in comments.

3 Comments

but echo to display the content of a file, shouldn't you have to pass that content as an argument to echo ie. echo "$cat file" >>filename ??
echo does NOT read from stdin, so echo < somefile >> file won't work, it will only append an empty line to file.
@mata: That was indeed a mistake, don't know why that happened. Anyway now the answer provides a way to do this with echo although that's not advisable; and shows to use tee as well..
2

Instead of cat, you can also use echo command to do the same.

And ofcourse, >> operator does it.

Comments

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.