歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> Linux文本處理小技巧

Linux文本處理小技巧

日期:2017/2/28 17:29:16   编辑:Linux教程

1、查看某文件的一部分

如果你只想看文件的前 5 行,可以使用 head 命令,

如:head -5 /etc/passwd

如果你想查看文件的後 10 行,可以使用 tail 命令,

如:tail -10 /etc/passwd

查看文件中間一段,可以使用 sed 命令

如:sed –n '5,10p' /etc/passwd 這樣你就可以只查看文件的第 5 行到第 10 行

2、將 file.txt 裡的123改為 456

方法 1

sed 's/123/456/g' file.txt > file.txt.new 修改的保存到其它文件

sed -i 's/123/456/g' file.txt 直接修改原文件

方法 2

vi file.txt

輸入命令:

:%s/123/456/g

注意:如果替換的文件有特殊符號如/就要用\來取消。

例:sed -i 's/\/usr\/local\/apache2\/htdocs/\/var\/www\/html/g' /usr/local/apache2/conf/httpd.conf

如果只是下原有的行後添加就用&

例:sed -i 's/DirectoryIndex index.html index.html.var/& index.htm index.php /g' /usr/local/apache2/conf/httpd.conf

3、echo 典型應用

echo "abcdefg" | perl -lne '{$a = reverse($_); print $a;}' 把一個字符串翻轉

echo bottle|rev 把一個字符串翻轉

Copyright © Linux教程網 All Rights Reserved