Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unix command to remove everything after first column

Tags:

linux

unix

sunos

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
like image 348
arsenal Avatar asked Jan 15 '13 03:01

arsenal


People also ask

How do you delete the first and last line in Unix?

1d deletes the first line ( 1 to only act on the first line, d to delete it) $d deletes the last line ( $ to only act on the last line, d to delete it)

How do I remove the first line from a header in Unix?

Using the sed Command Removing the first line from an input file using the sed command is pretty straightforward. The sed command in the example above isn't hard to understand. The parameter '1d' tells the sed command to apply the 'd' (delete) action on line number '1'.


1 Answers

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

like image 150
Mudassir Hasan Avatar answered Nov 16 '22 00:11

Mudassir Hasan