Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Linux文本操作 #10

Open
humingcheng opened this issue Aug 28, 2016 · 0 comments
Open

Linux文本操作 #10

humingcheng opened this issue Aug 28, 2016 · 0 comments

Comments

@humingcheng
Copy link
Owner

humingcheng commented Aug 28, 2016

显示第1行
sed -n 1p f1.sh # -n表示只显示操作的那几行,不然会显示整个文件

显示1,3行
sed -n '1p;3p' f1.sh
sed -n -e 1p -e 3p f1.sh # -e表示通知执行多个sed命令

显示1-3行
sed -n 1,3p f1.sh #

显示最后一行
sed -n '$p' f1.sh

显示含“love”且含”you“的行
grep love f1.sh | grep unix f1.sh

在第1行前插入1行’hello’内容:
sed -i '1i hello' f1.sh #-i表示直接写文件,否则只是写文件副本。

在第1行后插入2行’hello’,’world’内容:
sed -e '1a hello' -e '1a world' f1.sh

把第1行替换为hello:
sed "1c hello" f1.sh

将第1行中的unix替换为hello:
sed '2s/unix/hello/g' f1.sh #g表示替换匹配的全部,否则只会替换第一个匹配

将全文中的love替换为like:
sed 's/love/like/g' f1.sh #g表示替换一行中的所有匹配

在第1行首添加hello:
sed '1s/^/hello/' f1.sh #实际用了替换,^表示行首

在第1行尾添加hello:
sed "1s/$/hello/" f1.sh #$表示行尾

删除第1行的love:
sed "1s/love//g" f1.sh #替换为none

删除第1行:
sed '1d' f1.sh

未解决:
对倒数第2-4行进行操作:
对含love且不含you的行作操作:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant