cancel
Showing results for 
Search instead for 
Did you mean: 

grep the last string in a file

SOLVED
Go to solution
Vijaya Kumar_3
Respected Contributor

grep the last string in a file

I have the following file.

/tmp/portslist

*.22
*.53
*.80
*.443
*.22132
*.21112
*.22124
*.22059
*.49316
*.22023
*.22015
*.22056
*.22091
*.22133
*.22057
127.0.0.1.49181
*.22105
*.22011
*.22090
*.2121
*.22063
*.33505
*.22119
*.22092
*.22131
*.22024
*.22077
*.22106
*.22136
*.22107
127.0.0.1.50290
*.34543
127.0.0.1.51291


As you can see, the last pattern after the . (dot) is the port number. How can I just get only the port number from this file...

I want an output like this.

22
53
80
443
22132
21112 etc

Thanks in advance. I will assign points.

Regards
Vijay Chinnasamy
Known is a drop, unknown is ocean - visit me at http://vijay.theunixplace.com
14 REPLIES
R.K. #
Honored Contributor

Re: grep the last string in a file

Hello Vijay,

You can try this:

# cat /tmp/portslist | cut -d. -f2 > /tmp/ports

Regds..
Don't fix that what ain't broke
Vijaya Kumar_3
Respected Contributor

Re: grep the last string in a file

Thx for the reply.

The command you gave will give the second field after the dot. This will not work in case of the pattern 127.0.0.1.49181

I am looking for the last field after the dot, that is only the port number from this file...


Regards
Vijay Chinnasamy
Known is a drop, unknown is ocean - visit me at http://vijay.theunixplace.com
R.K. #
Honored Contributor
Solution

Re: grep the last string in a file

Hi Again..

Sorry for that..
Try this one:

# cat portslists | awk 'BEGIN {RS="\n";FS="."};{print $NF}'
Don't fix that what ain't broke
Patrick Wallek
Honored Contributor

Re: grep the last string in a file

No need to use cat and pipe the file into awk.

The above can be simplified to:

awk -F . '{print $NF}' /dir/filename
Hein van den Heuvel
Honored Contributor

Re: grep the last string in a file

If for some reason using "." as field separator does not work, or if you have processing for which PERL is more suitable, then you can use something like:

$ perl -lne 'print $1 if /\.(\d+)$/' file

That is a regular expression which looks for a period (escaped with \ ), followed by a number of digits (\d+) , remembered as $1 through those parens, and anchered at the end of the line with the $


The -l give a free new-line for each print (not needed whne processing furher) and he the -n tells perl to loop over an input file Not printing each line.

fwiw,
Hein.

Suraj K Sankari
Honored Contributor

Re: grep the last string in a file

Hi,

If your file is having only 2 field and field saperater is . then the command will be
#awk -F"." '{ print $NF }' outputfile

or

echo $filename | awk -F. '{print $NF}'

Suraj
Steven Schweda
Honored Contributor

Re: grep the last string in a file

sed -e 's/^.*\.\([^.]*$\)/\1/'

bash$ cat gt.txt
*.22133
*.22057
127.0.0.1.49181
*.22105

bash$ sed gt.txt -e 's/^.*\.\([^.]*$\)/\1/'
22133
22057
49181
22105


> grep the last string in a file

You mean _extract_ the last string in a file?
Who's using "grep"? Grep finds things. It
doesn't edit them. Don't choose a tool
until you understand what you wish to do with
it.
Dennis Handly
Acclaimed Contributor

Re: grep the last string in a file

>Hein: If for some reason using "." as field separator does not work

It works fine. One char field separators are fixed. If more than one, it is an ERE.

If you really want to make awk sweat with an ERE, you can use: -F'\.'
James R. Ferguson
Acclaimed Contributor

Re: grep the last string in a file

Hi Vijay:

Yet another way:

# perl -ne '@F=split /\./;print $F[@F-1]' file

Regards!

...JRF...
kobylka
Valued Contributor

Re: grep the last string in a file

Hi!


sed 's/.*\.//' /tmp/portslist


Kind regards,

Kobylka
Hein van den Heuvel
Honored Contributor

Re: grep the last string in a file

Dennis wrote:

>>Hein: If for some reason using "." as field separator does not work
>It works fine. One char field separators are fixed. If more than one, it is an ERE.

Right. Of course it works. I was not explicit enough.
I meant if it does not work for the exact usage.
You may have noticed how the questions and examples posted here do not always exactly match the real problem :-). Or perhaps a somewhat similar problem of a future reader.
The ERE can more readily be tweaked for more complex matches than a simple 'split'.

Jim wrote
>> perl -ne '@F=split /\./;print $F[@F-1]' file

Well, if you go that route, then you might as well use the build in autosplit ( -a + -F ):

$ perl -F'\.' -lane 'print $F[@F-1]' x

Hein

Laurent Menase
Honored Contributor

Re: grep the last string in a file

while read a
do
echo ${a##*.}
done
Vijaya Kumar_3
Respected Contributor

Re: grep the last string in a file

Thx everyone, I learned a lot from you guys in this thread.


Regards
Vijay Chinnasamy
Known is a drop, unknown is ocean - visit me at http://vijay.theunixplace.com
Steven Schweda
Honored Contributor

Re: grep the last string in a file

> sed 's/.*\.//' /tmp/portslist

_Much_ too simple. (I might _never_ think of
it.)