linux命令之⽂件查看和处理

一、基础查看

#查看⽂件内容
cat file1 

#查看内容并标示⾏数
cat -n file1 

#从最后⼀⾏开始反看⽂件内容
cat xxx.txt awk 'NR%2==1' tac file1 

#查看⼀个⻓⽂件的内容
more file1 

#类似more命令,但允许反向操作
less file1 

#查看⽂件前两⾏
head -2 file1

#查看⽂件后两⾏
tail -2 file1

#实时查看添加到⽂件中的内容
tail -f /log/msg

二、条件查看

#在⽂件hello.txt中查找关键词codesheep
grep codesheep hello.txt 

#在⽂件hello.txt中查找以sheep开头的内容
grep ^sheep hello.txt 

#选择hello.txt⽂件中所有包含数字的⾏
grep [0-9] hello.txt

#查看只包含关键词"s1"的⾏
sed -n '/s1/p' hello.txt


#查看从第⼀⾏到第5⾏内容
sed -n '1,5p;5q' hello.txt

#查看第5⾏
sed -n '5p;5q' hello.txt

三、替换和删除

#将hello.txt⽂件中的s1替换成s2
sed 's/s1/s2/g' hello.txt 

#从hello.txt⽂件中删除所有空⽩⾏
sed '/^$/d' hello.txt

#从hello.txt⽂件中删除所有注释和空⽩⾏
sed '/ *#/d; /^$/d' hello.txt

#从⽂件hello.txt 中排除第⼀⾏
sed -e '1d' hello.txt

#删除每⼀⾏最后的空⽩字符
sed -e 's/ *$//' hello.txt

#从⽂档中只删除词汇s1并保留剩余全部
sed -e 's/s1//g' hello.txt 

四、合并文件

#合并两个⽂件或两栏的内容
paste file1 file2

#合并两个⽂件或两栏的内容,中间⽤"+"区分
paste -d '+' file1 file2

五、排序和比较

#排序两个⽂件的内容
sort file1 file2

#uniq
sort file1 file2 

#uniq -u
sort file1 file2 

#uniq -d
sort file1 file2 

#⽐较两个⽂件的内容(去除'file1'所含内容)
comm -1 file1 file2 

#⽐较两个⽂件的内容(去除'file2'所含内容)
comm -2 file1 file2 

#⽐较两个⽂件的内容(去除两⽂件共有部分)
comm -3 file1 file2 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值