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

查看:284
本文介绍了如何使用 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来做到:

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

说明:

  • cut:调用剪切命令
  • -d " ":使用单个空格作为分隔符(cut默认使用TAB)
  • -f:指定要保留的字段
  • 3-:所有以字段3开头的字段
  • input_filename:使用这个文件作为输入
  • <代码>>output_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:

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

说明:

  • awk:调用awk命令
  • $1="";$2="";:将字段1和2设置为空字符串
  • sub(...);:清理输出字段,因为字段 1 &2 仍会以"分隔
  • print:打印修改后的行
  • input_filename >output_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天全站免登陆