• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

unix script to insert text at beginning of a file

kcthomas

Senior member
i wrote a script to edit some files and i want to add another command so that it inserts a couple lines of text to each file. how would i go about doing this? thanks

 
Well probably the easiest way to do it would be something like this:

Say you want some text inserted in a text file named file1

echo "this text you want at beginning of files" > file2
echo "this is another line of text you want" >> file2
cat file1 >> file2
mv file2 file1

Something like that.

Or if that isn't practical you can probably do something with "patch" and merge and stuf like that, like you do when you apply a patch to a peice of source code.

GNU Diffutils manual
 
Never done unix scripting, but in windows scripting you would have to read the contents of the file to a variable, empty the file, write the new lines, then write the contents of the original file back. Only way I know to insert lines at the beginning of a file. End of file is a hell of a lot easier 🙂.
 
Back
Top