Very small shell: Specify line number, insert text

xiaoxiao2021-03-06  117

Using this shell can insert the specified text directly in a certain line of a file, it is very convenient. You can add this shell into the path. The following blue part is code, the file name is insert [cheyo @ Samsung Test] $ more insert

#! / bin / bash

# Insert a new line from a certain line of the file

# File is the file name, linenum is the line number, Text is the text to be inserted.

# Copyleft: Cheyo @ JMU

Showusage ()

{

echo "[usage]: Insert File Linenum Text

echo "# Insert a new line from a certain line of the file"

Echo "# file is the file name, linenum is the line number, Text is the text to be inserted"

}

IF [$ # -lt 3] # less than 3 parameters, error!

Then Showusage

EXIT 1

Else

Sed "$ {2} a //

$ {3} "$ 1> TMPFILE; MV TMPFILE $ 1

Fi [Cheyo @ Samsung Test] $ more a

Line1 line2 line3 line4 [Cheyo @ Samsung Test] $ ./insert a 3 "TEST" [Cheyo @ Samsung Test] $ more a

LINE1 line2 line3 test line4

[cheyo @ Samsung Test] $

转载请注明原文地址:https://www.9cbs.com/read-98985.html

New Post(0)