Linux Unix 命令删除第一列之后的所有内容

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14330655/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 18:40:15  来源:igfitidea点击:

Unix command to remove everything after first column

linuxunixsunos

提问by arsenal

I have a text file in which I have something like this-

我有一个文本文件,其中有这样的内容-

10.2.57.44      56538154    3028
120.149.20.197  28909678    3166
10.90.158.161   869126135   6025

In that text file, I have around 1,000,000 rows exactly as above. I am working in SunOS environment. I needed a way to remove everything from that text file leaving only IP Address (first column in the above text file is IP Address). So after running some unix command, file should look like something below.

在那个文本文件中,我有大约 1,000,000 行,与上面完全一样。我在 SunOS 环境中工作。我需要一种方法来从该文本文件中删除所有内容,只留下 IP 地址(上述文本文件中的第一列是 IP 地址)。所以在运行一些 unix 命令后,文件应该看起来像下面这样。

10.2.57.44
120.149.20.197
10.90.158.161

Can anyone please help me out with some Unix command that can remove all the thing leaving only IP Address (first column) and save it back to some file again.

任何人都可以用一些 Unix 命令帮助我,它可以删除所有只留下 IP 地址(第一列)的东西,然后再次将其保存回某个文件。

So output should be something like this in some file-

所以在某些文件中输出应该是这样的 -

10.2.57.44
120.149.20.197
10.90.158.161

采纳答案by shellter

 nawk '{print }' file > newFile && mv newFile file

OR

或者

 cut -f1 file > newFile && mv newFile file

As you're using SunOS, you'll want to get familiar with nawk (not awk, which is the old, and cranky version of awk, while nawk= new awk ;-).

当您使用 SunOS 时,您需要熟悉 nawk(而不是 awk,它是 awk 的旧版本和古怪版本,而 nawk= new awk ;-)。

In either case, you're printing the first field in the file to newFile.

无论哪种情况,您都将文件中的第一个字段打印到 newFile。

(n)awk is a complete programming language designed for the easy manipulation of text files. The $1means the first field on each line, $9 would mean the ninth field, etc, while $0 means the whole line. You can tell (n)awk what to use to separate the fields by, it might be a tab char, or a '|' char, or multiple spaces. By default, all versions of awk uses white space, i.e. multiple spaces, or 1 tab to delimit the columns/fields, per line in a file.

(n)awk 是一种完整的编程语言,旨在轻松处理文本文件。该$1方法在每一行的第一个字段,$ 9将意味着第九场等,而$ 0表示整行。您可以告诉 (n)awk 使用什么来分隔字段,它可能是制表符或“|” 字符或多个空格。默认情况下,awk 的所有版本都使用空格,即多个空格或 1 个制表符来分隔文件中每行的列/字段。

For a very good intro to awk, see Grymtheitroade's Awk page

有关 awk 的非常好的介绍,请参阅Grymtheitroade 的 Awk 页面

The &&means, execute the next command only if the previous command finished without a problem. This way you don't accidentally erase your good data file, becuase of some error.

&&手段,只执行如果先前的命令完成没有问题的下一个命令。这样你就不会因为一些错误而意外删除你的好数据文件。

IHTH

IHTH

回答by Suku

awk '{ print  }' file_name.txt > tmp_file_name.txt
mv tmp_file_name.txt file_name.txt

'> tmp_file_name.txt'means redirecting STDOUTof awk '{ print $1 }' file_name.txtto a file named tmp_file_name.txt

'> tmp_file_name.txt'来重新定位STDOUTawk '{ print $1 }' file_name.txt一个命名文件tmp_file_name.txt

FYI :

供参考 :

 means first column based on delimiter. The default delimiter is whitespace
 means second column based on delimiter. The default delimiter is whitespace
..
..
$NR means last column based on delimiter. The default delimiter is whitespace

If you want to change delimiter, use awkwith -F

如果要更改分隔符,请使用awkwith-F

回答by Mudassir Hasan

If delimiter is space character use

如果分隔符是空格字符使用

 cut -d " " -f 1 filename

If delimiter is tab character , no need for -d option as tab is default delimiter for cutcommand

如果分隔符是制表符,则不需要 -d 选项,因为制表符是cut命令的默认分隔符

cut -f 1 filename

-dDelimiter; the character immediately following the -d option is the field delimiter .

-d分隔符;紧跟在 -d 选项之后的字符是字段分隔符。

-fSpecifies a field list, separated by a delimiter

-f指定字段列表,由分隔符分隔

回答by Mukund K Roy

If you have vim, open the file with it. Then in command mode write for substitution (tab or space or whatever is the delimiter) %s:<delimiter>.*$::g. Now save the file with :wq.

如果有vim,请用它打开文件。然后在命令模式下写入替换(制表符或空格或任何分隔符)%s:<delimiter>.*$::g。现在用:wq.

Using sedgive command like this sed -e 's/<delimiter>.*$//' > file.txt

使用sed像这样的命令sed -e 's/<delimiter>.*$//' > file.txt

回答by DavidG

How about a perl script ;)

perl 脚本怎么样 ;)

#!/usr/bin/perl -w
use strict;

my $file = shift;
die "Missing file or can't read it" unless $file and -r $file;

sub edit_in_place
{
    my $file       = shift;
    my $code       = shift;
    {
        local @ARGV = ($file);
        local $^I   = '';
        while (<>) {
            &$code;
        }
    }
}

edit_in_place $file, sub {
    my @columns = split /\s+/;
    print "$columns[0]\n";
};

This will edit the file in place since you say it is a large one. You can also create a backup by modifying local $^I = '';to local $^I = '.bak';

这将就地编辑文件,因为您说它是一个大文件。您还可以通过修改local $^I = '';来创建备份local $^I = '.bak';

回答by Mirage

Try this

尝试这个

awk '{$1=$1; print $1}' temp.txt

awk '{$1=$1; print $1}' temp.txt

Output

输出

10.2.57.44
120.149.20.197
10.90.158.161