- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- grep the last string in a file
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-23-2009 04:52 PM
тАО09-23-2009 04:52 PM
/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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-23-2009 05:11 PM
тАО09-23-2009 05:11 PM
Re: grep the last string in a file
You can try this:
# cat /tmp/portslist | cut -d. -f2 > /tmp/ports
Regds..
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-23-2009 05:19 PM
тАО09-23-2009 05:19 PM
Re: grep the last string in a file
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-23-2009 05:42 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-23-2009 05:49 PM
тАО09-23-2009 05:49 PM
Re: grep the last string in a file
The above can be simplified to:
awk -F . '{print $NF}' /dir/filename
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-23-2009 06:29 PM
тАО09-23-2009 06:29 PM
Re: grep the last string in a file
$ 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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-23-2009 06:39 PM
тАО09-23-2009 06:39 PM
Re: grep the last string in a file
If your file is having only 2 field and field saperater is . then the command will be
#awk -F"." '{ print $NF }'
or
echo $filename | awk -F. '{print $NF}'
Suraj
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-23-2009 07:20 PM
тАО09-23-2009 07:20 PM
Re: grep the last string in a file
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-24-2009 01:51 AM
тАО09-24-2009 01:51 AM
Re: grep the last string in a file
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'\.'
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-24-2009 03:26 AM
тАО09-24-2009 03:26 AM
Re: grep the last string in a file
Yet another way:
# perl -ne '@F=split /\./;print $F[@F-1]' file
Regards!
...JRF...