- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- get rid of firs four characters
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
тАО01-16-2009 08:28 AM
тАО01-16-2009 08:28 AM
I have a file which looks something like this:
00042 #include "common.h"
00043 #include "utils.h"
00044 #include "popen.h"
I need to get rad of first 5 or 6 numerical values.
My system is Linux.
Can you help ?
Regards
Peter
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-16-2009 08:39 AM
- Tags:
- Perl
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-16-2009 10:12 AM
тАО01-16-2009 10:12 AM
Re: get rid of firs four characters
Could you please step by step explain the code. I understand you are using regular expressions but I'm not very good with them.
regards
Peter
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-16-2009 10:45 AM
тАО01-16-2009 10:45 AM
Re: get rid of firs four characters
Yes, I'd be happy to explain. Perl has superior regular expression handling.
# perl -pi.old -e 's/^\d+//' file
The '-p creates a read loop for us. The '-i' says to do inplace updates preserving the original file renamed with a suffix of ".old". What follows the '-e' is the script.
We want to (s)ubstitute one or more digits (\d+) with nothing. Too, we want our match (if any) to be anchored to the beginning of a line (^) so that we don't arbitrarily change strings of digits anywhere.
It would be useful to amend our code to allow leading (optional) whitespace too:
# perl -pi.old -e 's/^\s*\d+//' file
The '\s*' expressions denotes zero or more occurances of whitespace --- blanks and/or tabs.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-16-2009 10:50 AM
тАО01-16-2009 10:50 AM
Re: get rid of firs four characters
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-16-2009 10:59 AM
тАО01-16-2009 10:59 AM
Re: get rid of firs four characters
> I didn't get what // stands for
It is the "replace" part of the "search" in the substitution. If you like, we can use something other than the '/' delimiter.
# perl -pi.old -e 's{^\d+}{}' file
This does two things for clarity. First, it elimintes the foward and backward slashes which the eye can find hard to read in some patterns. Next, in this case, it makes it very easy to see the "search" part (the pattern to match) and the "replace"ment part or the string to substitute for the match.
Using the '/' character delimiter as I originally did does allow a bit of shorthand --- the one less delimiter.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-16-2009 02:03 PM - edited тАО09-04-2011 09:30 PM
тАО01-16-2009 02:03 PM - edited тАО09-04-2011 09:30 PM
Re: get rid of leading line numbers
>I need to get rid of first 5 or 6 numerical values.
If you want to get rid of the space after number you can use:
sed -e 's/^[0-9]* //' file > file.new
This finds leading numbers followed by a space then removes them.
You can also use similar syntax in vi.
- Tags:
- sed
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-16-2009 03:00 PM
тАО01-16-2009 03:00 PM
Re: get rid of firs four characters
Indeed to trim the leading digits on each line whether or not they are prefixed and/or suffixed with optional whitespace, simply do:
# perl -pi.old -e 's/^\s*\d+\s*//' file
or:
# perl -pi.old -e 's{^\s*\d+\s*}{}' file
Regards!
...JRF...