mtt*_*pgn 3 sed awk perl regular-expression cut
我正在寻找一种方法来提取没有特定分隔符的文本文件的第一列,除了开始下一列的任意数字。例子:
John Smith 1234 Main Street
Amy Brown and Sally Williams 9 Drury Lane
Sunny's 1000 Brown Avenue
Run Code Online (Sandbox Code Playgroud)
预期输出将是:
John Smith
Amy Brown and Sally Williams
Sunny's
Run Code Online (Sandbox Code Playgroud)
似乎cut不支持功能,例如cut file.txt -d {0..9} -f 1
解决方案可以使用任何标准的 Unix 实用程序。
$ awk -F'[0-9]' '{ print $1 }' file
John Smith
Amy Brown and Sally Williams
Sunny's
Run Code Online (Sandbox Code Playgroud)
随着-F'[0-9]'我们说数字是在输入数据被认为是场分离,并与print $1我们输出的第一位分开场。
更改-F'[0-9]'为-F' *[0-9]'也去掉数字前的任何空格。
和一个sed解决方案:
echo "John Smith 1234 Main Street
Amy Brown and Sally Williams 9 Drury Lane
Sunny's 1000 Brown Avenue" | sed 's/ *[0-9].*$//'
John Smith
Amy Brown and Sally Williams
Sunny's
Run Code Online (Sandbox Code Playgroud)