Add a line in a specific position with Linux and output to the same file?
The only way to do this is to write to a second file, then replace the original. You can only append to an arbitrary file; you cannot insert into the middle of one.
t=$(mktemp)
sed '3iline 3' file.txt > "$t" && mv "$t" file.txt
If your version of sed
supports it, you can use the -i
option to automate the handling of the temporary file.
sed -i '3iline 3' file.txt # GNU
sed -i "" '3iline 3 ' file.txt # BSD sed requires an argument for -i
Bash: Inserting a line in a file at a specific location
If you want to add a line after a specific string match:
$ awk '/master.mplayer.com/ { print; print "new line"; next }1' foo.input
ServerActors=IpServer.UdpServerUplink MasterServerAddress=unreal.epicgames.com MasterServerPort=27900
ServerActors=IpServer.UdpServerUplink MasterServerAddress=master0.gamespy.com MasterServerPort=27900
ServerActors=IpServer.UdpServerUplink MasterServerAddress=master.mplayer.com MasterServerPort=27900
new line
ServerActors=UWeb.WebServer
Insert line after match using sed
Try doing this using GNU sed:
sed '/CLIENTSCRIPT="foo"/a CLIENTSCRIPT2="hello"' file
if you want to substitute in-place, use
sed -i '/CLIENTSCRIPT="foo"/a CLIENTSCRIPT2="hello"' file
Output
CLIENTSCRIPT="foo"
CLIENTSCRIPT2="hello"
CLIENTFILE="bar"
Doc
- see sed doc and search
\a
(append)
sed insert line with spaces to a specific line
You can escape the space
character, for example to add 2 spaces:
sed -i "${line} i \ \ ${text}" $file
Or you can do it in the definition of your text
variable:
text="\ \ hello world"
Simple way to insert line to particular position in file
awk 'f==1{print last}{last=$0;f=1}END{print "NEW WORD\n"$0}' file
sed/awk Insert lines in specific place in file
Try the following simple sed
command (GNU sed
)
sed 's@</body>@\t<!-- EX -->\n\t<A href="ex/live/current/index.html" >EX Live</A> \n\t<A href="ex/live/" >(All months)</A><br>\n</body>@'
No need to use /
as delimiter, it can be what you want instead, here we have the separator @
A PORTABLE SOLUTION
(tested on Solaris 11
, FreeBSD 8.0
and Archlinux
)
sed 's@</body>@ <!-- EX -->\
<A href="ex/live/current/index.html" >EX Live</A> \
<A href="ex/live/" >(All months)</A><br>\
</body>@' file.html
How to add text with sed at the end-of-line minus 1 position
sed "/^ *resourceFilters: '/s/'$/[*,test,*]'/" your_file
- I've double quoted the Sed command so that I can use single quotes in it;
/^resourceFilters: '/
makes the followings
ubstitution command only act on lines starting withresourceFilters: '
;s/'$/[*,test,*]'/
is the substitution command which matches'
at end of line ($
), and substitutes[*, test,*]'
to it, effectively inserting[*,test,*]
before the'
.
Related Topics
How to Get Details of All Modules/Drivers That Were Initialized/Probed During the Linux Kernel Boot
How to Connect a Shell to a Pseudo Tty
Makefile Export .O File to a Different Path Than .Cpp
What Is Difference Between Arm64 and Armhf
Macros for Gcc/G++ to Differentiate Linux and MAC Osx
What Is the Linux Process Table? What Does It Consist Of
List File Using Ls Command in Linux with Full Path
Shell-Init: Error Retrieving Current Directory: Getcwd -- the Usual Fixes Do Not Wor
How to Automate Telnet Session Using Expect
Chrome Certificate Selection Appears Multiple Times
How to Detect Usb Drive Insertion in Linux
Printing Variable to Command Line Using Assembly in Linux
How to Find a List of Ip Addresses in Another File
Error: Can't Open Display: (Null) When Using Xclip to Copy Ssh Public Key