25

如何在 shell 中剪切字符串的第一列(可变长度)?

字符串前:

23006 帮助.txt

我需要 23006 作为输出

4

3 回答 3

45

很多种方法:

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
于 2012-12-27T08:59:04.083 回答
6
cut -d " " -f1 test.txt

其中 test.txt 包含您的输入行

于 2012-12-27T09:00:25.247 回答
6

这个对我有用

cut -d' ' -f2-
于 2018-10-31T08:19:47.483 回答