简体   繁体   中英

unable to insert text at a particular line of a file using linux sed

I want to replace the 2nd line of a file using linux sed using these following commands.

sed -i"" '2d' /usr/local/services/status.sh
sed -i '2i\testing' /usr/local/services/status.sh

Is there any error in my command? I can't find out. Is there any other way to do it?

Any kind of help is appreciated.

这可能对您有用:

sed -i '2c\testing' file

To replace 2nd line:

$ cat file
AIX
Unix
Linux
$ sed -i '2s/.*/testing/' file
$ cat file
AIX
testing
Linux

To insert before the 2nd line:

$ sed -i '2i testing' file

To use a variable as part of subtitution:

$ PID=2456
$ sed -i "2s/.*/testing $PID/" file

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM