Commonly Used Linux Commands


1. Sed

Here is an example test.txt file:

> cat test.txt
hello world
hello world
hello world
hello world
hello world
hello world

1.1. Insert / Append

# Insert before the second row
> sed '2i\haha' test.txt
hello world
haha
hello world
hello world
hello world
hello world
hello world

# Insert after the third row
> sed '3a\hahaha' test.txt
hello world
hello world
hello world
hahaha
hello world
hello world
hello world

# Insert before the last row
> sed '$i\hahahah' test.txt
hello world
hello world
hello world
hello world
hello world
hahahah
hello world

1.2. Delete

# Delete the context at second row
> sed '2d' test.txt
hello world
hello world
hello world
hello world
hello world

1.3. Replace

# Replace the first (by default) 'l' by 'a' for all rows (by default)
> sed 's/l/a/' test.txt
healo world
healo world
healo world
healo world
healo world
healo world

# Replace the second 'l' by 'x' for all rows
> sed 's/l/x/2' test.txt
helxo world
helxo world
helxo world
helxo world
helxo world
helxo world

# Replace all the matches of 'l' by 'x' for all rows
> sed 's/l/x/g' test.txt
hexxo worxd
hexxo worxd
hexxo worxd
hexxo worxd
hexxo worxd
hexxo worxd

# Repace the third 'l' by 'x' only for second row.
> sed '2s/l/x/3' test.txt
hello world
hello worxd
hello world
hello world
hello world
hello world

# Replace and modify the source files, all the 'l' by 'x' for the third row.
> sed -i '3s/l/x/g' test.txt
> cat test.txt
hello world
hello world
hexxo worxd
hello world
hello world
hello world

2. Awk

awk is a powerful text analysis tool. The command is:

> awk [POSIX or GNU style options] [--] 'program' file ...

Let’s use an example file test.txt:

> cat test.txt
hello world
hello world
hexxo worxd
hello world
heaao worad
hello world

2.1. Read different columns

The results of different columns are stored in $1, $2, $3 and etc. $0 stores the whole information.

> awk '{print $1}' test.txt
hello
hello
hexxo
hello
heaao
hello

> awk '{print $2}' test.txt
world
world
worxd
world
worad
world

> awk '{print $0}' test.txt
hello world
hello world
hexxo worxd
hello world
heaao worad
hello world

2.2. Use customized separator

# Use ":" to separate columns
> awk -F: '{print $1}' /etc/passwd
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
operator
games
ftp
nobody
systemd-network
dbus
polkitd
sshd
postfix
chrony
nscd
tcpdump

2.3. Filter results by regular expressions

# Only show rows that contain "world"
> awk '/world/ {print $0}' test.txt
hello world
hello world
hello world
hello world

# Only shows rows that matches "world" for a specific column
# No results to match the first column
> awk '($1 ~ /world/) {print $0}' test.txt

> awk '($2 ~ /world/) {print $0}' test.txt
hello world
hello world
hello world
hello world

> awk '($2 ~ /wor[a-z]d/) {print $0}' test.txt
hello world
hello world
hexxo worxd
hello world
heaao worad
hello world
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

aliengod

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值