文本处理大全

一、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:退格符
  • \:反斜杠
  • \?:问号
  • \':单引号
  • \":双引号

十、常见文本处理案例

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值