Help: How to delete the first character from a line?

A

Amy Lee

Hello,

There's a file like this,

;348577;aaaaaaaaaaaaaaaaa;3454353453;asdssssssssssss;234323;asdassssssss
dadssdasef;ttgfhdfg

I hope it can be this,

348577;aaaaaaaaaaaaaaaaa;3454353453;asdssssssssssss;234323;asdassssssss
dadssdasef;ttgfhdfg

So my problem is how use Perl to delete the first character of the first
line. Use \s?

Thank you very much~

Regards,

Amy Lee
 
P

Paul Lalli

There's a file like this,

;348577;aaaaaaaaaaaaaaaaa;3454353453;asdssssssssssss;234323;asdassssssss
dadssdasef;ttgfhdfg

I hope it can be this,

348577;aaaaaaaaaaaaaaaaa;3454353453;asdssssssssssss;234323;asdassssssss
dadssdasef;ttgfhdfg

So my problem is how use Perl to delete the first character of the first
line. Use \s?

I have no idea how the space metacharacter would help you here.

I would just use four-arg substr.

perl -lpi.bkp -e'substr($_, 0, 1, "");' file.txt

perldoc -f substr

There Is, of course, More Than One Way To Do It. Other ways include:
search-and-replace: s/^.//;
reverse/chomp: $_ = reverse $_; chomp; $_ = reverse $_;
three-arg substr: substr($_, 0, 1) = "";

If one of those makes more sense to you than the others, go for it. I
prefer the substr.

Paul Lalli
 
A

anno4000

Amy Lee said:
Hello,

There's a file like this,

;348577;aaaaaaaaaaaaaaaaa;3454353453;asdssssssssssss;234323;asdassssssss
dadssdasef;ttgfhdfg

I hope it can be this,

348577;aaaaaaaaaaaaaaaaa;3454353453;asdssssssssssss;234323;asdassssssss
dadssdasef;ttgfhdfg

So my problem is how use Perl to delete the first character of the first
line. Use \s?

Your question is not very clear. Do you want to change the disk file
so that the leading semicolon is no longer there? Or do you want to
keep it in the file, but remove it from the line before processing the
line further?

What do you mean with "use \s"? "\s" denotes white space in a regular
expression, but that won't help deleting a semicolon.

Assuming you want to keep the file as it is, and assuming that $fh is
a readable file handle to your file (untested):

while ( <$fh> ) {
s/^;// if $. == 1; # only on the first line
# further processing
}

Anno
 
P

Paul Lalli

Hrm. I didn't register the "of the first line" part of this when I
answered. My answer was presuming you wanted to delete the first
character of each line.
I would just use four-arg substr.

perl -lpi.bkp -e'substr($_, 0, 1, "");' file.txt

Simply add a check in there to make sure you only do this to the first
line:

perl -lpi.bkp -e'substr($_, 0, 1, "") if $. == 0;' file.txt

or, slurp the entire file at once rather than reading/writing line-by-
line (not recommended for large files):

perl -0 0777 -pi.bkp -e'substr($_, 0, 1, "");' file.txt

Paul Lalli
 
A

Amy Lee

Your question is not very clear. Do you want to change the disk file
so that the leading semicolon is no longer there? Or do you want to
keep it in the file, but remove it from the line before processing the
line further?

What do you mean with "use \s"? "\s" denotes white space in a regular
expression, but that won't help deleting a semicolon.

Assuming you want to keep the file as it is, and assuming that $fh is
a readable file handle to your file (untested):

while ( <$fh> ) {
s/^;// if $. == 1; # only on the first line
# further processing
}

Anno

Thank you sir, and $. means amount of lines?

Regards,

Amy Lee
 
G

Gunnar Hjalmarsson

Amy said:
There's a file like this,

;348577;aaaaaaaaaaaaaaaaa;3454353453;asdssssssssssss;234323;asdassssssss
dadssdasef;ttgfhdfg

I hope it can be this,

348577;aaaaaaaaaaaaaaaaa;3454353453;asdssssssssssss;234323;asdassssssss
dadssdasef;ttgfhdfg

So my problem is how use Perl to delete the first character of the first
line.

open my $file, '+<', 'myfile.txt' or die $!;
my @lines = <$file>;
$lines[0] = substr $lines[0], 1;
seek $file, 0, 0;
truncate $file, 0;
print $file @lines;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top