如何使用shell来删除一个文件中的前两列(AWK,sed的,等等) [英] how to remove the first two columns in a file using shell (awk, sed, whatever)

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

问题描述

我有很多行的文件
每行有很多列(字段)用空格分开
列的每行中的数字是不同的
我想删除前两列
怎么样?

I have a file with many lines in each line there are many columns(fields) separated by blank " " the numbers of columns in each line are different I want to remove the first two columns how to?

推荐答案

您可以用剪切

cut -d " " -f 3- input_filename > output_filename

说明:


  • 剪切:调用cut命令

  • -d:使用一个空格作为分隔符(削减默认使用TAB)

  • -f :指定领域保持

  • 3 - :所有的领域开始与现场3

  • input_filename :使用此文件作为输入

  • >输出文件名:将输出写入该文件

  • cut: invoke the cut command
  • -d " ": use a single space as the delimiter (cut uses TAB by default)
  • -f: specify fields to keep
  • 3-: all the fields starting with field 3
  • input_filename: use this file as the input
  • > output_filename: write the output to this file.

另外,你可以用 AWK 做到这一点:

Alternatively, you can do it with awk:

awk '{$1=""; $2=""; sub("  ", " "); print}' input_filename > output_filename

说明:


  • AWK :调用awk命令

  • $ 1 =; $ 2 =; :设置场1和2为空字符串

  • 子(...); :清理输出字段,因为字段1安培; 2将仍然被,分隔

  • 打印:打印修改行

  • input_filename>输出文件名:同上

  • awk: invoke the awk command
  • $1=""; $2="";: set field 1 and 2 to the empty string
  • sub(...);: clean up the output fields because fields 1 & 2 will still be delimited by " "
  • print: print the modified line
  • input_filename > output_filename: same as above.

这篇关于如何使用shell来删除一个文件中的前两列(AWK,sed的,等等)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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