delete only part of a line

Hi,

I've a file as below-

I need to delete only the part of the line after the pattern 'extent /lock' and my output has to be like below.

Thanks

perl -pnle 's/(extent|lock).*(.)$/$2/g' inputfile

Thanks Pravin!

That does exactly what i need, can u pls give a brief explanation on logic behind it, as im new to perl.

And also, can u pls suggest me some good books for perl.

Thanks.

there's no need to use backrefereneces.

$ ruby -ne 'print $_.gsub(/(lock|extent).*$/,"")' file
perl -pnle 's/(extent|lock).*(.)$/$2/g' inputfile

The -e switch allows to write Perl scripts directly on the command line.
The -n switch wraps a while loop around your program
The -p switch prints the value of $_ at the end of each iteration.
The -l switch print newline.

s/(extent|lock).*(.)$/$2/g
  • Substitute "extent OR (|) lock followed by any character till second last character, (.) last character." with last character.i.e. group 2

I got a problem here, there are few cases where it is split in to multiple lines.. But i need to remove the content till it finds a ; symbol.

Thanks

There is no need to specify "-n" when you already have "-p". "-p" is a superset of "-n" :slight_smile:

perl -pe 'BEGIN{$/="\;";} s/(extent|lock).*(.)/$2/sg;' inputfile