如何使用 shell(awk、sed 等)删除文件中的前两列 [英] how to remove the first two columns in a file using shell (awk, sed, whatever)
本文介绍了如何使用 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 keep3-
: all the fields starting with field 3input_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 stringsub(...);
: clean up the output fields because fields 1 & 2 will still be delimited by " "print
: print the modified lineinput_filename > output_filename
: same as above.
这篇关于如何使用 shell(awk、sed 等)删除文件中的前两列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文