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

Har*_*K L 12 linux shell

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

字符串ex:

23006 help.txt

我需要23006作为输出

ani*_*ane 25

很多种方法:

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
Run Code Online (Sandbox Code Playgroud)


hon*_*jde 6

这个对我有用

cut -d' ' -f2-
Run Code Online (Sandbox Code Playgroud)


Inc*_*ito 5

cut -d " " -f1 test.txt
Run Code Online (Sandbox Code Playgroud)

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