① add a line to the top of the file with sed:
Sed-i ' 1s/^/added line \n/' filename
Note: sed-i ' 1s/^/added line/' filename, no \ n ' means adding a sentence before the first line of the file
such as: Comment out the first line sed-i ' 1s/^/#/' filename
② inserting multiple rows of data into a file using the command line:
Cat >> filename << EOF
Xxxxxxxxxxxxxxxxxxx
Xxxxxxxxxxxxxxxxxx
Xxxxxxxxxxxxxxxxx
Eof
Note: EOF is a shorthand for end of file, inserting data between two EOF into a file
③ use Find and sed to manipulate files:
Find. -type f-name ' *.txt '-exec sed-i ' s/jeff/tom/g ' {} \; Find the file at the end of the current directory and replace Jeff in the file with Tom
Note: You can create a function in the script file, such as:
function find_sed
{
Find. -type f-name ' *.txt '-exec sed-i ' s/$1/$2/g ' {} \;
}
Find_sed Jeff Tom #调用函数 and passed in two external variables Jeff and Tom
For more information see this link: http://www.techug.com/ten-tips-for-wonderful-bash-productivity
This article from "See" blog, declined reprint!
10 productivity-Boosting bash techniques