I have the scenario where lines to be added on begining and end of the huge files.I have tried as shown below.for the first line: sed -i '1i\'$FirstLine $Filenamefor the last line: sed -i '$ a\'$Lastline $Filename  But the issue with this command is that it is appending the first line of the file and traversing entire file. For the last line it's again traversing the entire file and appending a last line.  Since its very huge file (14GB) this is taking very long time.How can I add a line to the beginning and another to the end of a file while only reading the file once?
Add lines to the beginning and end of the huge file
shell script;files
sed -i uses tempfiles as an implementation detail, which is what you are experiencing; however, prepending data to the beginning of a data stream without overwriting the existing contents requires rewriting the file, there's no way to get around that, even when avoiding sed -i.If rewriting the file is not an option, you might consider manipulating it when it is read, for example:{ echo some prepended text ; cat file ; } | commandAlso, sed is for editing streams  -- a file is not a stream. Use a program that is meant for this purpose, like ed or ex. The -i option to sed is not only not portable, it will also break any symlinks to your file, since it essentially deletes it and recreates it, which is pointless.You can do this in a single command with ed like so:ed -s file << 'EOF'0aprepend these linesto the beginning.$aappend these linesto the end.wEOFNote that depending on your implementation of ed, it may use a paging file, requiring you to have at least that much space available.