Thursday, April 28, 2011

copy one file content to another using "sed" editor

I am using shell script. How to copy one file content to another file using "sed". I have to use only sed to complete this copy. can anyone help me?

Thanks in advance Sudha

From stackoverflow
  • sed writes its output on standard output, not in a file. You have to use redirections. One possible command is:

    sed -n 'p' myInFile > myOutFile
    
  • All you need is a noop:

    sed -e '' <"${OLDNAME}" >"${NEWNAME}"
    

    ...that said, why? Most tiny embedded systems will be using busybox, and while they may not have cp (hey, I've built a busybox without one for a dedicated router appliance!), not adding cat to the busybox config is just silly.

  • As suggested, you can do this with sed, but why not simply copy the file with the cp command?

    Charles Duffy : That much I can understand -- cp isn't a shell builtin, and so may not be available in embedded systems either (1) not using busybox or (2) with an extremely cut-down busybox. Granted, sed isn't either, but it may already be installed for other reasons. Still silly -- the shell alone is enough.
  • If you actually wanted to use the write functionality in sed, you could use the w command to write to a specific file

    sed -n "w$NEWNAME" $OLDNAME
    

0 comments:

Post a Comment