我有一个文件ma.txt
,它包含ls -l
; 的输出;当我运行tr
命令(tr "nik-pc" "root"
)时,得到以下输出:
nik-pc@nik:~$ cat ma.txt
total 52
drwxr-xr-x 2 nik-pc nik-pc 4096 Mar 11 11:33 Desktop
lrwxrwxrwx 1 nik-pc nik-pc 2 Mar 8 22:54 di -> hd
drwxr-xr-x 3 nik-pc nik-pc 4096 Mar 13 13:28 Documents
drwxr-xr-x 7 nik-pc nik-pc 4096 Mar 14 18:21 Downloads
drwxr-xr-x 2 nik-pc nik-pc 4096 Mar 11 09:39 dwhelper
-rw-r--r-- 1 nik-pc nik-pc 2134 Mar 13 17:40 hd
-rw-r--r-- 1 nik-pc nik-pc 3 Mar 13 15:34 m
-rw-r--r-- 1 nik-pc nik-pc 0 Mar 17 19:48 ma.txt
drwxr-xr-x 3 nik-pc nik-pc 4096 Mar 13 14:58 Music
drwxr-xr-x 2 nik-pc nik-pc 4096 Mar 5 12:30 Pictures
drwxr-xr-x 2 nik-pc nik-pc 4096 Mar 5 11:44 Public
drwxr-xr-x 2 nik-pc nik-pc 4096 Mar 13 15:58 sd
drwxr-xr-x 2 nik-pc nik-pc 4096 Mar 5 11:44 Templates
drwxr-xr-x 2 nik-pc nik-pc 4096 Mar 5 11:44 Videos
drwxr-xr-x 2 nik-pc nik-pc 4096 Mar 11 11:33 xdm-helper
nik-pc@nik:~$ tr "nik-pc" "root" < ma.txt
tttat 52
drwxr-xr-x 2 too-tt too-tt 4096 Mar 11 11:33 Desottt
trwxrwxrwx 1 too-tt too-tt 2 Mar 8 22:54 do -> hd
drwxr-xr-x 3 too-tt too-tt 4096 Mar 13 13:28 Dttutetts
drwxr-xr-x 7 too-tt too-tt 4096 Mar 14 18:21 Dtwtttads
drwxr-xr-x 2 too-tt too-tt 4096 Mar 11 09:39 dwhetter
-rw-r--r-- 1 too-tt too-tt 2134 Mar 13 17:40 hd
-rw-r--r-- 1 too-tt too-tt 3 Mar 13 15:34 t
-rw-r--r-- 1 too-tt too-tt 0 Mar 17 19:48 ta.txt
drwxr-xr-x 3 too-tt too-tt 4096 Mar 13 14:58 Musot
drwxr-xr-x 2 too-tt too-tt 4096 Mar 5 12:30 Pottures
drwxr-xr-x 2 too-tt too-tt 4096 Mar 5 11:44 Pubtot
drwxr-xr-x 2 too-tt too-tt 4096 Mar 13 15:58 sd
drwxr-xr-x 2 too-tt too-tt 4096 Mar 5 11:44 Tetttates
drwxr-xr-x 2 too-tt too-tt 4096 Mar 5 11:44 Vodets
drwxr-xr-x 2 too-tt too-tt 4096 Mar 11 11:33 xdt-hetter
在第一行,它用“ too”代替了“ nik”,“ Desktop”的拼写变成了“ Desottt”。
为什么是这样?它背后的逻辑是什么?
nik-pc
为root
?
n
它也在k-p
范围内。POSIX未指定字符在第一组上出现多次的结果。
sed
命令…… ls -l | sed 's/nik-pc/root/'
会做您想要的。 tr
用于转换单个字符-例如。小写到大写,或dos样式的lineshift(\ r)到Unix样式的(\ n),或诸如用斜杠替换所有反斜杠(Windows)之类的东西。它还可以删除“不需要的”字符,例如。所有数字或所有大写字母。
info coreutils 'tr invocation'
告诉您详细信息tr
。