LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   get only first line using sed / awk (https://www.linuxquestions.org/questions/programming-9/get-only-first-line-using-sed-awk-946567/)

iffarrukh 05-24-2012 01:30 AM

get only first line using sed / awk
 
Hi I have a file which contains IP address like this


192.168.28
192.168.28
192.168.28
192.168.28
192.168.28
192.168.28
192.168.28
0.0.0.0
0.0.0.0
0.0.0.0
192.168.8.
0.0.0.0
192.168.8.
0.0.0.0
0.0.0.0
0.0.0.0
0.0.0.0
0.0.0.0
192.168.28
192.168.28
192.168.28
192.168.28
192.168.28
0.0.0.0
192.168.28
192.168.28
192.168.28
192.168.28
192.168.28
192.168.28
0.0.0.0


I want to get only first IP address. I know they are all same but I need only one ( in the bold) using sed/awk or any other comamnd..

Any help would be great.

Thanks.
Sam

pan64 05-24-2012 01:44 AM

have you tried sort -u or uniq?

colucix 05-24-2012 01:51 AM

To retrieve the first line of a file there are plenty of methods:
Code:

$ head -n1 file
$ sed -n 1p file
$ awk 'NR == 1' file

If you want to put the result in a variable, use command substitution.

David the H. 05-24-2012 06:13 PM

There's no need for an external program either.

Code:

read var <infile ; echo "$var"


All times are GMT -5. The time now is 05:29 AM.