linux vim 终端 行首 行尾_Linux 常见问题解答(下)

ec5511c53752af096aac3b5156195e85.png

17. 如何使用管道将上游的数据传入 Vim 编辑器?

```

grep -v "localhost" /etc/hosts | vim –`

cat aa.txt | vim -

# 或者

vim <(cat aa.txt)

```

18. 如何将程序的 stdout 和 stderr 通过管道输出到两个文件?

这涉及到输出的重导向,分别用1和2将标准输出和标准错误输出到两个文件中

```{bash eval=F}

# 把标准错误输出到stderr.txt

cat example.txt 2>stderr.txt

# 把标准输出输出到stdout.txt

cat example.txt 1>stdout.txt

# 把标准错误以标准输出形式输出

cat example.txt 2>&1

# 把标准输出以标准错误形式输出

cat example.txt 1>&2

```

参考资料:

<https://mp.weixin.qq.com/s/_MLfjwcXrVo3H9V6iwbGew>

<https://www.jstorimer.com/blogs/workingwithcode/7766119-when-to-use-stderr-instead-of-stdout>

<https://my.oschina.net/qihh/blog/55308>

<https://www.zhihu.com/question/24030687>

<https://stackoverflow.com/questions/16497317/piping-both-stdout-and-stderr-in-bash><http://note.qidong.name/2017/07/bash_stdout_stderr/>

<https://blog.csdn.net/wangyeqiang/article/details/38726433>

<http://www.01happy.com/linux-stdin-stdout-stderr/>

<https://zh.wikipedia.org/zh/%E6%A8%99%E6%BA%96%E4%B8%B2%E6%B5%81>

19. 如何查看隐藏文件

```

l.

ls -d .*

ls -a

```

20. 如何使用 ROOT 权限?如何切换不同的身份

`sudo` 可以使非管理员执行需要root权限的操作;使用`su`可以进行不同身份的切换。

```

sudo mount /dev/sdb1 /mnt/m1

su root;su username`

```

`$`变成了`#`,说明现在是使用 root 用户

21. 如果输入的shell脚本过长,快速删除有哪些快捷键呢?如何快速调整光标位置

- Ctrl + l 清屏

- Ctrl + a:移到行首(a是首字母)

- Ctrl + e:移到行尾(结束)

- Ctrl + xx:行首到当前光标替换

- Ctrl + u: 从光标所在位置一直删除到开头

- Ctrl + k: 从光标所在位置一直删除到尾

- Ctrl + a 移动光标到行首

- Ctrl + e 移动光标到行尾

- Ctrl + b 往后移动一个字符

- Ctrl + f 往前移动一个字符

- Ctrl + A:光标跳到命令行起始位置

- Ctrl + E:光标跳到命令行末尾位置

- Ctrl + B:向后移动一个字符

- Ctrl + F:向前移动一个字符

- Alt + 左箭头:向前移动一个单词

- Alt + 右箭头:向后移动一个单词

22. Linux 文件操作时,比较 awk/grep/sed 三剑客的用法

grep 更适合单纯的查找或匹配文本,sed 更适合编辑匹配到的文本,awk 更适合格式化文本,对文本进行较复杂格式处理。

三个命令的运用形式:

grep '字符' 文件

sed '命令' 文件

awk '条件{命令}' 文件

```{bash eval = FALSE}

awk ‘{}’ file

grep this file

sed ‘2a hello’ file

```

参考资料:

<https://www.jianshu.com/p/b3574d31f579>

<https://www.jianshu.com/p/879e22bedfb8><https://www.jianshu.com/p/9fcda404d9e1><https://www-users.york.ac.uk/~mijp1/teach

ing/2nd_year_Comp_Lab/guides/grep_awk_sed.pdf><https://thief.one/2017/08/12/1/><https://segmentfault.com/a/1190000015885994><https://unix.stackexchange.com/questions/2434/is-there-a-basic-tutorial-for-grep-awk-and-sed><https://stackoverflow.com/questions/7727640/what-are-the-differences-among-grep-awk-sed>

<http://blog.cee.moe/a-brief-introduction-to-grep-awk-and-sed.html>

<https://www.jianshu.com/p/3af864e9cb5a>

23. Linux 系统中哪个文件是黑洞,

`/dev/null` 通常用来处理进程中那些不想要的输出流,或者作为一个方便的空文件给输入流。这个通过用来做重定向。

参考资料:

<https://askubuntu.com/questions/12098/what-does-outputting-to-dev-null-accomplish-in-bash-scripts><https://zh.wikipedia.org/wiki//dev/null><http://blog.jobbole.com/109355/>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值