linux查看⽇志⽂件内容命令tail、cat、tac、head、echo linux查看⽇志⽂件内容命令tail、cat、tac、head、echo
tail -f test.log
你会看到屏幕不断有内容被打印出来. 这时候中断第⼀个进程Ctrl-C,
---------------------------
linux 如何显⽰⼀个⽂件的某⼏⾏(中间⼏⾏)
从第3000⾏开始,显⽰1000⾏。即显⽰3000~3999⾏
cat filename | tail -n +3000 | head -n 1000
显⽰1000⾏到3000⾏
cat filename| head -n 3000 | tail -n +1000
*注意两种⽅法的顺序
分解:
tail -n 1000:显⽰最后1000⾏
tail -n +1000:从1000⾏开始显⽰,显⽰1000⾏以后的
head -n 1000:显⽰前⾯1000⾏
⽤sed命令
sed -n '5,10p' filename 这样你就可以只查看⽂件的第5⾏到第10⾏。
例:cat mylog.log | tail -n 1000 #输出mylog.log ⽂件最后⼀千⾏
---------------------------
cat主要有三⼤功能:
1.⼀次显⽰整个⽂件。$ cat filename
2.从键盘创建⼀个⽂件。$ cat > filename
只能创建新⽂件,不能编辑已有⽂件.
3.将⼏个⽂件合并为⼀个⽂件: $cat file1 file2 > file
参数:
-n 或 --number 由 1 开始对所有输出的⾏数编号
-b 或 --number-nonblank 和 -n 相似,只不过对于空⽩⾏不编号
-s 或 --squeeze-blank 当遇到有连续两⾏以上的空⽩⾏,就代换为⼀⾏的空⽩⾏
-v 或 --show-nonprinting
例:
把 textfile1 的档案内容加上⾏号后输⼊ textfile2 这个档案⾥
cat -n textfile1 > textfile2
把 textfile1 和 textfile2 的档案内容加上⾏号(空⽩⾏不加)之后将内容附加到 textfile3 ⾥。
cat -b textfile1 textfile2 >> textfile3
把⽂件扔进垃圾箱,赋空值
cat /dev/null > /
注意:>意思是创建,>>是追加。千万不要弄混了。
------------------------------------------
tac (反向列⽰)
tac 是将 cat 反写过来,所以他的功能就跟 cat 相反, cat 是由第⼀⾏到最后⼀⾏连续显⽰在萤幕上,
⽽ tac 则是由最后⼀⾏到第⼀⾏反向在萤幕上显⽰出来!
------------------------------------------
在Linux中echo命令⽤来在标准输出上显⽰⼀段字符,⽐如:
echo "the echo command test!"
这个就会输出“the echo command test!”这⼀⾏⽂字!
linux怎么读取文件内容
echo "the echo command test!">a.sh
这个就会在a.sh⽂件中输出“the echo command test!”这⼀⾏⽂字!
该命令的⼀般格式为: echo [ -n ] 字符串其中选项n表⽰输出⽂字后不换⾏;字符串能加引号,也能不加引号。
⽤echo命令输出加引号的字符串时,将字符串原样输出;
⽤echo命令输出不加引号的字符串时,将字符串中的各个单词作为字符串输出,各字符串之间⽤⼀个空格分割。