ShellScript : How to write to the beginning of a file in Linux

How to write to the beginning of a file using shell script in Linux

Suppose, we have a file release.txt and we want to write to first line of the of the file whenever a new release is made.

sed -i '1i\'"text to write " filename 

Using the above command as sed -i '1i\'"newVersion-RELEASE" release.txt
This will append the text to the first line of the file release.txt

In the example shown above , we have added a new version 2.0.0-RELEASE to version.txt file .

If you want to do this using a shell script then you can do it as follows


function writeAtBeginning(){
    sed -i '1i\'"$1" $2

writeAtBeginning "$@"

In the above example, we have added the new version through a shell function by passing version and file name as parameters

No comments :

Post a Comment