cut 和 awk 中默认分隔符和字段计算的区别
difference in default delimiter and field calculation in cut and awk
我正在尝试缩短进程启动的时间。
这是 ps 行。似乎由 spaces 分隔。
casper@casperhost02 1065$ ps -ef | grep [t]xg casper 5345 1 34 16:40 ? 00:06:56 q /casper_apps/casper/reader/q/txg_dr.q -g 1 -w 80000 -p 3041 -taq /casper_apps/casper/data/fiofs/hdb0 -bin /casper_apps/casper/reader/q -t1 /casper_apps/casper/data/file -cmq /casper_apps/casper/common/q -ref /casper_apps/casper/static -prg txg_dr
我只想缩短 ps 行的时间并按字段提取..
当我使用 cut and delimit by space 时,时间在字段 10.
casper@casperhost02 1075$ ps -ef | grep [t]xg | cut -d' ' -f10
16:40
然而,当我使用 awk 时,它的 id 默认由 space 分隔,它是字段 5。
casper@casperhost02 1076$ ps -ef | grep [t]xg | awk '{print }'
16:40
awk和cut中的字段分隔符有什么区别?
cut
统计space,awk
统计字段,默认将所有连续的白色space视为一个分隔符。请注意,如果您已经在使用 awk
,则不需要 grep
... | awk '/txg/{print }'
您还可以将正则表达式(或文字)匹配限制到正确的字段并消除误报。
cut
接受单字符定界符,仅适用于非常简单的文本文件格式。
Awk 更通用,可以处理更复杂的字段分隔符定义,甚至(在某些方言中)正则表达式。默认情况下,Awk 将 序列 空格视为单个分隔符。
顺便说一下,您似乎在寻找命令 pgrep
。如果您必须重新发明它,请记住 grep x | awk '{y}'
几乎总是可以写成 awk '/x/{y}'
.
我正在尝试缩短进程启动的时间。 这是 ps 行。似乎由 spaces 分隔。
casper@casperhost02 1065$ ps -ef | grep [t]xg casper 5345 1 34 16:40 ? 00:06:56 q /casper_apps/casper/reader/q/txg_dr.q -g 1 -w 80000 -p 3041 -taq /casper_apps/casper/data/fiofs/hdb0 -bin /casper_apps/casper/reader/q -t1 /casper_apps/casper/data/file -cmq /casper_apps/casper/common/q -ref /casper_apps/casper/static -prg txg_dr
我只想缩短 ps 行的时间并按字段提取.. 当我使用 cut and delimit by space 时,时间在字段 10.
casper@casperhost02 1075$ ps -ef | grep [t]xg | cut -d' ' -f10
16:40
然而,当我使用 awk 时,它的 id 默认由 space 分隔,它是字段 5。
casper@casperhost02 1076$ ps -ef | grep [t]xg | awk '{print }'
16:40
awk和cut中的字段分隔符有什么区别?
cut
统计space,awk
统计字段,默认将所有连续的白色space视为一个分隔符。请注意,如果您已经在使用 awk
grep
... | awk '/txg/{print }'
您还可以将正则表达式(或文字)匹配限制到正确的字段并消除误报。
cut
接受单字符定界符,仅适用于非常简单的文本文件格式。
Awk 更通用,可以处理更复杂的字段分隔符定义,甚至(在某些方言中)正则表达式。默认情况下,Awk 将 序列 空格视为单个分隔符。
顺便说一下,您似乎在寻找命令 pgrep
。如果您必须重新发明它,请记住 grep x | awk '{y}'
几乎总是可以写成 awk '/x/{y}'
.