Operating System - HP-UX
1753783 Members
6907 Online
108799 Solutions
New Discussion юеВ

get rid of firs four characters

 
SOLVED
Go to solution
Piotr Kirklewski
Super Advisor

get rid of firs four characters

Hi there

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


Jesus is the King
7 REPLIES 7
James R. Ferguson
Acclaimed Contributor
Solution

Re: get rid of firs four characters

Hi Peter:

One way:

# perl -pi.old -e 's/^\d+//' file

...which trims any leading digits (0-9). A backup copy of your file will be made as "file.old" and an inplace update done.

Regards!

...JRF...
Piotr Kirklewski
Super Advisor

Re: get rid of firs four characters

Thanks :)
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
Jesus is the King
James R. Ferguson
Acclaimed Contributor

Re: get rid of firs four characters

Hi (again) Peter:

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...
Piotr Kirklewski
Super Advisor

Re: get rid of firs four characters

I didn't get what // stands for ?
Jesus is the King
James R. Ferguson
Acclaimed Contributor

Re: get rid of firs four characters

Hi (again) Peter:

> 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...


Dennis Handly
Acclaimed Contributor

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.

James R. Ferguson
Acclaimed Contributor

Re: get rid of firs four characters

Hi (again) Peter:

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...