使用 sed 从文本文件中删除列 [英] Deleting columns from text files with sed

查看:82
本文介绍了使用 sed 从文本文件中删除列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从空格分隔的文本文件中删除第 1、2、4、5、6、10、11、12 列.我想从现有文件中删除这些列而不是打印输出.我怎样才能用 sed 做到这一点?

I would like to delete columns 1,2,4,5,6,10,11,12 from space delimited text files.I want to delete these columns from the existing files instead of printing output. How can I do this with sed?

MMMM   1522  KL1 PPP A 201      -7.299   41.933  48.192  1.00 31.52           G
MMMM     22  G   SSS A   3       39.541  25.078  -2.722  1.00 30.47           B  

期望的输出

KL1    -7.299   41.933  48.192
G       39.541  25.078  -2.722

推荐答案

sed 并不理想.使用cut:

cut -d ' ' --complement -f -2,4-6,10-12 file.txt

来自评论中的其他信息:

From additional information from the comments:

< file.txt awk '{ print $3, $7, $8, $9 }' | column -t

结果:

KL1  -7.299  41.933  48.192
G    39.541  25.078  -2.722

要覆盖您的文件,您需要使用一个临时文件:

To overwrite your file, you'll need to use a temporary file:

< file.txt awk '{ print $3, $7, $8, $9 }' | column -t > tmpfile && mv tmpfile file.txt

这篇关于使用 sed 从文本文件中删除列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆