开发者

linux command line: cut (with empty fields)

开发者 https://www.devze.com 2023-04-08 02:32 出处:网络
I have a file (input.txt) with columns of data separated by spaces. I want to get the 9th column of data and onwards.

I have a file (input.txt) with columns of data separated by spaces. I want to get the 9th column of data and onwards.

Normally I would 开发者_运维技巧do:

cut -d " " -f 9- input.txt

However, in this file, sometimes the fields are separated by multiple spaces (and the number of spaces varies for each row / column). cut doesn't seem to treat consecutive spaces as one delimiter.

What should I do instead?


sed -r 's/ +/ /g' input.txt|cut -d " " -f 9-


You could use sed to replace n whitespaces with a single whitespace:

sed -r 's/\ +/\ /g' input.txt | cut -d ' ' -f 9-

Just be sure there aren't any tabs between your columns.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号