如何在shell中剪切字符串的第一列(变长)?
字符串示例:
23006 help.txt
我需要输出23006。
字符串示例:
23006 help.txt
我需要输出23006。
许多方式:
cut -d' ' -f1 <filename # If field separator is space
cut -f1 <filename # If field separator is tab
cut -d' ' -f1 <filename | cut -f1 # If field separator is space OR tab
awk '{print $1}' filename
while read x _ ; do echo $x ; done < filename
cut -d " " -f1 test.txt
其中test.txt包含您的输入行
这个对我有效
cut -d' ' -f2-
ps
命令会给出格式化的输出(就像printf %5d
一样)。因此,当cut
使用空格作为分隔符时,它会将前导空格(对于较小的 PID 数字)视为分隔符。因此,它会打印第一个空格之前的所有内容(在这种情况下为空)。 - anishsane