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

时间:2021-01-17 12:26:50

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

6 个解决方案

#1


7  

 nawk '{print $1}' 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 $1 means 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的所有版本都使用空格(即多个空格或一个选项卡)来分隔文件中的每行列/字段。

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

要获得非常好的awk介绍,请参阅格兰杰的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

#2


14  

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 cut command

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

cut -f 1 filename

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

- d分隔符;紧接-d选项的字符是字段分隔符。

-f Specifies a field list, separated by a delimiter

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

#3


1  

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: <分隔符> .*$:g。现在将文件保存为:wq。

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

使用这样的sed give命令-e 's/ <分隔符> 。* / / ' > file.txt美元

#4


1  

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';

这将在适当的位置编辑文件,因为您说它是一个大文件。您还可以创建一个备份通过修改本地$ ^ = ";当地$ ^我= . bak的;

#5


1  

Try this

试试这个

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

awk的{ 1美元= 1美元;打印$ 1 }’temp.txt

Output

输出

10.2.57.44
120.149.20.197
10.90.158.161

#6


0  

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

'> tmp_file_name.txt' means redirecting STDOUT of awk '{ print $1 }' file_name.txt to a file named tmp_file_name.txt

“> tmp_file_name。txt'意味着将STDOUT从awk '{print $1}' file_name重定向。txt到名为tmp_file_name.txt的文件

FYI :

仅供参考:

$1 means first column based on delimiter. The default delimiter is whitespace
$2 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 awk with -F

如果要更改分隔符,请使用带-F的awk

#1


7  

 nawk '{print $1}' 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 $1 means 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的所有版本都使用空格(即多个空格或一个选项卡)来分隔文件中的每行列/字段。

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

要获得非常好的awk介绍,请参阅格兰杰的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

#2


14  

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 cut command

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

cut -f 1 filename

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

- d分隔符;紧接-d选项的字符是字段分隔符。

-f Specifies a field list, separated by a delimiter

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

#3


1  

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: <分隔符> .*$:g。现在将文件保存为:wq。

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

使用这样的sed give命令-e 's/ <分隔符> 。* / / ' > file.txt美元

#4


1  

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';

这将在适当的位置编辑文件,因为您说它是一个大文件。您还可以创建一个备份通过修改本地$ ^ = ";当地$ ^我= . bak的;

#5


1  

Try this

试试这个

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

awk的{ 1美元= 1美元;打印$ 1 }’temp.txt

Output

输出

10.2.57.44
120.149.20.197
10.90.158.161

#6


0  

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

'> tmp_file_name.txt' means redirecting STDOUT of awk '{ print $1 }' file_name.txt to a file named tmp_file_name.txt

“> tmp_file_name。txt'意味着将STDOUT从awk '{print $1}' file_name重定向。txt到名为tmp_file_name.txt的文件

FYI :

仅供参考:

$1 means first column based on delimiter. The default delimiter is whitespace
$2 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 awk with -F

如果要更改分隔符,请使用带-F的awk