print last five characters with PERL regex


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting print last five characters with PERL regex
# 1  
Old 01-25-2010
print last five characters with PERL regex

greetings citizens of Unix.com

I am perplexed with an issue.
The issue is trying to print the last 5 characters of a string in PERL.

Below are demonstrated my daft attempts at performing the forementioned task.

Code:
$row[3] =~ m/^.*(.....)\s$/;

Code:
$row[3] =~ m/\w{5}\s*$/i;

Code:
$row[3] =~ s/(^0{2,})(\w{5}$)/\2/o;

Code:
my $serialno =  $row->[3];
print $serialno."\n";



---------- Post updated at 10:43 AM ---------- Previous update was at 09:23 AM ----------

I gave up and went this route ...

Code:
        my $length = length($row->[3]);
        my $start = $length - 5;
    my $serialno = substr($row->[3], $start, $length);

# 2  
Old 01-25-2010
Hello,

The following works for me.

Code:
perl -wl -e 'my $str=<>;chomp $str;$str=~/^.*(.{5})$/ and print $1'

See an illustration below ->
Code:
gaurav@localhost:~$ perl -wl -e 'my $str=<>;chomp $str;$str=~/^.*(.{5})$/ and print $1'
myhouseisinindia
india
gaurav@localhost:~$

Pretty much works ..eh..??

Regards,
Gaurav.
# 3  
Old 01-25-2010
This seems to work too:
Code:
$ perl -wl -e 'my $str=<>;$str=~/(.{5})$/ and print $1'
myhouseisinamsterdam
erdam

# 4  
Old 01-25-2010
Quote:
Originally Posted by Scrutinizer
This seems to work too:
Code:
$ perl -wl -e 'my $str=<>;$str=~/(.{5})$/ and print $1'
myhouseisinamsterdam
erdam

Hey buddy..... thats my copyright. lol Smilie
works nonetheless hehe.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Regex to identify illegal characters in a perso-arabic database

I am working on Sindhi: a perso-Arabic script and since it shares the Unicode-block with over 400 other languages, quite often the database contains characters which are not wanted: illegal characters. I have identified the character set of Sindhi which is given below: For clarity's sake, each... (8 Replies)
Discussion started by: gimley
8 Replies

2. Shell Programming and Scripting

Grep string with regex numeric characters

Hi all, I have the following entries in a file: Cause Indicators=80 90 Cause Indicators=80 90 Cause Indicators=82 90 Cause Indicators=82 90 Cause Indicators=82 90 The first 2 digits might change so I am after a sort of grep which could find any first 2 digits + the second 2,... (3 Replies)
Discussion started by: nms
3 Replies

3. Shell Programming and Scripting

Grep with Regex multiple characters

Hi everyone, So I'm a bit confused about a regex pattern that should exist, but I can't really find any way to do it... Let's say I want to match any lines that have a specific string, but I don't know the order of the letters but I know the length. Let's say it's 10 characters and begins... (3 Replies)
Discussion started by: Lost in Cyberia
3 Replies

4. Shell Programming and Scripting

Perl, RegEx - Help me to understand the regex!

I am not a big expert in regex and have just little understanding of that language. Could you help me to understand the regular Perl expression: ^(?!if\b|else\b|while\b|)(?:+?\s+){1,6}(+\s*)\(*\) *?(?:^*;?+){0,10}\{ ------ This is regex to select functions from a C/C++ source and defined in... (2 Replies)
Discussion started by: alex_5161
2 Replies

5. UNIX for Dummies Questions & Answers

read regex from ID file, print regex and line below from source file

I have a file of protein sequences with headers (my source file). Based on a list of IDs (which are included in some of the headers), I'd like to print out only the specified sequences, with only the ID as header. In other words, I'd like to search source.txt for the terms in IDs.txt, and print... (3 Replies)
Discussion started by: pathunkathunk
3 Replies

6. Shell Programming and Scripting

Nawk - print only a string containing a regex.

Hi again, I'm looking for some help with nawk, I can print a line which has a regex match in it from a file using /pattern/ but I'm looking for a way to only print the $tring which contains the pattern, rather than the whole line. This $tring may be of variable length, may occur at any point... (1 Reply)
Discussion started by: spynappels
1 Replies

7. Shell Programming and Scripting

Print lines after regex

Hello, I need to print four lines inmediatly after the regexp, but not the line containing the regexp. The print should show the four lines together in one. Thanks! (13 Replies)
Discussion started by: auratus42
13 Replies

8. Shell Programming and Scripting

Converting perl regex to sed regex

I am having trouble parsing rpm filenames in a shell script.. I found a snippet of perl code that will perform the task but I really don't have time to rewrite the entire script in perl. I cannot for the life of me convert this code into something sed-friendly: if ($rpm =~ /(*)-(*)-(*)\.(.*)/)... (1 Reply)
Discussion started by: suntzu
1 Replies

9. Shell Programming and Scripting

print first few lines, then apply regex on a specific column to print results.

abc.dat tty cpu tin tout us sy wt id 0 0 7 3 19 71 extended device statistics r/s w/s kr/s kw/s wait actv wsvc_t asvc_t %w %b device 0.0 133.2 0.0 682.9 0.0 1.0 0.0 7.2 0 79 c1t0d0 0.2 180.4 0.1 5471.2 3.0 2.8 16.4 15.6 15 52 aaaaaa1-xx I want to skip first 5 line... (4 Replies)
Discussion started by: kchinnam
4 Replies

10. Shell Programming and Scripting

sed - print only matching regex

Hi folks, Lets say I have the following text file: name, lastname, 1234, name.lastname@test.com name1, lastname1, name2.lastname2@test.com, 2345 name, 3456, lastname, name3.lastname3@test.com 4567, name, lastname, name4.lastname4@test.com I now need the following output: 1234... (5 Replies)
Discussion started by: domi55
5 Replies
Login or Register to Ask a Question