سؤال

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.

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.

So output should be something like this in some file-

10.2.57.44
120.149.20.197
10.90.158.161
هل كانت مفيدة؟

المحلول 2

 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 ;-).

In either case, you're printing the first field in the file to 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.

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

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

نصائح أخرى

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

cut -f 1 filename

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

-f Specifies a field list, separated by a delimiter

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.

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

How about a perl script ;)

#!/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';

Try this

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

Output

10.2.57.44
120.149.20.197
10.90.158.161
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

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

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top