一、grep
二、awk
awk '{$1=""; sub(/^ +/, ""); print}' xxx.txt
三、sed
sed常用案例
示例文本
Every morning, she enjoys a cup of tea while reading the latest news.
They decided to take a vacation to unwind after months of hard work.
Children often learn more from what they see than what they are told.
The book has an intriguing plot that keeps readers engaged until the last page.
Despite the heavy rain, the farmers were happy as it promised a good harvest.
Technology has greatly changed the way we communicate with one another.
It’s important to set goals that challenge you but are still achievable.
Learning a new language opens doors to different cultures and opportunities.
3.1 指定某个关键字的开头或者结尾替换文字
替换以Technology开头行的with为of
sed '/^Technology/s/with/of/' text.txt
Every morning, she enjoys a cup of tea while reading the latest news.
They decided to take a vacation to unwind after months of hard work.
Children often learn more from what they see than what they are told.
The book has an intriguing plot that keeps readers engaged until the last page.
Despite the heavy rain, the farmers were happy as it promised a good harvest.
Technology has greatly changed the way we communicate of one another.
It’s important to set goals that challenge you but are still achievable.
Learning a new language opens doors to different cultures and opportunities.
替换以page.结尾行的last为first
sed '/page.$/s/last/first/' text.txt
Every morning, she enjoys a cup of tea while reading the latest news.
They decided to take a vacation to unwind after months of hard work.
Children often learn more from what they see than what they are told.
The book has an intriguing plot that keeps readers engaged until the first page.
Despite the heavy rain, the farmers were happy as it promised a good harvest.
Technology has greatly changed the way we communicate with one another.
It’s important to set goals that challenge you but are still achievable.
Learning a new language opens doors to different cultures and opportunities.
3.2 全局替换所有关键字
替换所有 the 为 eht
sed 's/ the / eht /g' text.txt
Every morning, she enjoys a cup of tea while reading eht latest news.
They decided to take a vacation to unwind after months of hard work.
Children often learn more from what they see than what they are told.
The book has an intriguing plot that keeps readers engaged until eht last page.
Despite eht heavy rain, eht farmers were happy as it promised a good harvest.
Technology has greatly changed eht way we communicate with one another.
It’s important to set goals that challenge you but are still achievable.
Learning a new language opens doors to different cultures and opportunities.
3.3 打印只被更改的行
sed -n '/^Technology/s/with/of/p' text.txt
Technology has greatly changed the way we communicate of one another.
3.4 行首或行尾插入字符串
对以Despite开头的一行行首插入Head
sed '/^Despite/s/^/Head&/' text.txt
Every morning, she enjoys a cup of tea while reading the latest news.
They decided to take a vacation to unwind after months of hard work.
Children often learn more from what they see than what they are told.
The book has an intriguing plot that keeps readers engaged until the last page.
HeadDespite the heavy rain, the farmers were happy as it promised a good harvest.
Technology has greatly changed the way we communicate with one another.
It’s important to set goals that challenge you but are still achievable.
Learning a new language opens doors to different cultures and opportunities.
对以Despite开头的一行行尾插入Tail
sed '/^Despite/s/$/&Tail/' text.txt
Every morning, she enjoys a cup of tea while reading the latest news.
They decided to take a vacation to unwind after months of hard work.
Children often learn more from what they see than what they are told.
The book has an intriguing plot that keeps readers engaged until the last page.
Despite the heavy rain, the farmers were happy as it promised a good harvest.Tail
Technology has greatly changed the way we communicate with one another.
It’s important to set goals that challenge you but are still achievable.
Learning a new language opens doors to different cultures and opportunities.
四、通配符
五、正则表达式
六、转义序列字符
- \0:空字符
- \n:换行符
- \r:回车符
- \t:水平制表符
- \v:垂直制表符
- \a:响铃
- \f:换页符
- \b:退格符
- \:反斜杠
- \?:问号
- \':单引号
- \":双引号