Stack Exchange Network
Stack Exchange network consists of 181 Q&A communities including
Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.
Visit Stack Exchange
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.
Sign up to join this community
Anybody can ask a question
The best answers are voted up and rise to the top
10 years ago
In a bash script I want to write some lines to a text file, but this file has already been used before and there are texts in it. So I want to echo some additional text in it, starting from a certain line number in the file.
I want something like this:
echo -fromLineNumber 33 -e "anything" >> textPath
77.8k 42 42 gold badges 164 164 silver badges 221 221 bronze badges
May 14, 2013 at 17:15
You can use
sed to write at a particular line.
sed -i '33ianything' textpath
sed -i '33i\anything' textpath
It will insert "anything" in line number 33.
May 14, 2013 at 17:35
436 5 5 silver badges 10 10 bronze badges
To keep the first 32 lines and add new text afterwards:
head -n 32 oldfile > newfile
echo anything >> newfile
echo goes >> newfile
echo here >> newfile
To insert some text after line 32 of a file:
sed -e '32s/$/\nanything\ngoes\nhere/' oldfile > newfile
May 14, 2013 at 17:24
46.6k 5 5 gold badges 111 111 silver badges 156 156 bronze badges
log in to answer this question.
Not the answer you're looking for? Browse other questions tagged
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our
Accept all cookies
Necessary cookies only