perl script to split the filename


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting perl script to split the filename
# 1  
Old 08-02-2012
perl script to split the filename

In PERL script
I have few files named theme1.htm,theme2.htm,theme3.htm and so on.

now I need to write perl code to split the the filename and store only that particular digit.

Example
--------------
filename is theme1.htm
output should be 1

another example
---------------
filename is theme3.htm
output should be 3


Could you please help me in writing the perl code.

Thanks in advance....
# 2  
Old 08-02-2012
Code:
 
$ echo "theme3.htm" | perl -lane 's/theme(\d+).htm/$1/;print $_' 
3
$ echo "theme150.htm" | perl -lane 's/theme(\d+).htm/$1/;print $_'
150

This User Gave Thanks to itkamaraj For This Post:
# 3  
Old 08-02-2012
Another example (Perl)
============

$var = "theme1.htm";

From the above variable 1 has to be seperated and should be stored
in another variable. i.e., the output should be 1
# 4  
Old 08-02-2012
In unix..
try like...
Code:
 echo "theme1.htm" | tr -dc '[0-9]'

This User Gave Thanks to bmk For This Post:
# 5  
Old 08-02-2012
Code:
 
#!/bin/perl
$var="theme108.htm";
$var=~m/theme(\d+).htm/;
$anothervar=$1;
print $anothervar;

This User Gave Thanks to itkamaraj For This Post:
# 6  
Old 08-02-2012
Many thanks kamaraj.

I am writing the script in perl and here the filename is assigned to a variable.
So Could you please make use of variable which contains the filename and then split it.

$filename = "theme1.htm";
Now I need to print only 1.

---------- Post updated at 01:23 AM ---------- Previous update was at 01:20 AM ----------

Many thanks kamaraj ... It worked... Smilie

ThanQ bmk...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Modification of perl script to split a large file into chunks of 5000 chracters

I have a perl script which splits a large file into chunks.The script is given below use strict; use warnings; open (FH, "<monolingual.txt") or die "Could not open source file. $!"; my $i = 0; while (1) { my $chunk; print "process part $i\n"; open(OUT, ">part$i.log") or die "Could... (4 Replies)
Discussion started by: gimley
4 Replies

2. Shell Programming and Scripting

perl script to split the text file after every 4th field

I had a text file(comma seperated values) which contains as below 196237,ram,25-May-06,ram.kiran@xyz.com,204183,Pavan,4-Jun-07,Pavan.Desai@xyz.com,237107,ram Chandra,15-Mar-10,ram.krishna@xyz.com ... (3 Replies)
Discussion started by: giridhar276
3 Replies

3. Shell Programming and Scripting

Split Filename from Absolute Path

Hello, I have the string "/a/b/c/ddd.txt" and i want to get only the filename, in this case "ddd.txt". I have as something known in the script the pattern "/a/b/c/", so I`ve tried something like: echo "/a/b/c/ddd.txt" | cut -d "/a/b/c/" -f2 but it doesn`t go, any help?. thanks, bye (2 Replies)
Discussion started by: rubber08
2 Replies

4. Shell Programming and Scripting

Perl script error to split huge data one by one.

Below is my perl script: #!/usr/bin/perl open(FILE,"$ARGV") or die "$!"; @DATA = <FILE>; close FILE; $join = join("",@DATA); @array = split( ">",$join); for($i=0;$i<=scalar(@array);$i++){ system ("/home/bin/./program_name_count_length MULTI_sequence_DATA_FILE -d... (5 Replies)
Discussion started by: patrick87
5 Replies

5. Shell Programming and Scripting

how to get split output of a file, using perl script

Hi, I have file: data.log.1 ### s1 main.build.3495 main.build.199 main.build.3408 ###s2 main.build.3495 main.build.3408 main.build.199 I want to read this file and store in two arrays in Perl. I have following command, which is working fine on command prompt. perl -n -e... (1 Reply)
Discussion started by: ashvini
1 Replies

6. Shell Programming and Scripting

directory -l filename perl

Can anybody let me know what following commands will do in perl 1.my $result = `/main/home/bin/iwex -l '$File1'`; 2.my $setcmd = "/main/home/bin/iwex -s \"$File2\" \"$File3\""; where $File1 $File2 $File3 are regular files. Please suggest something. Ur welcome (4 Replies)
Discussion started by: millan
4 Replies

7. AIX

split a filename and print to 2 different headings

I need help to split a filename 'a0crk_user:A0-B0123$#%test' into a0crk_user and A0-B0123 and print the output under 2 different columns namely User and Status. for eg. the output should like below: User Status ---- ------ a0crk_user A0-B0123 (3 Replies)
Discussion started by: mbak
3 Replies

8. Shell Programming and Scripting

Perl Script : Split given month into weeks

I want to split a given month into weeks. For example if I give the date in dd/mm/yy format say 01/02/08 it should give output in the given format : week1 : start date and end date. week2 : "" week3 : "" week4 : "" (5 Replies)
Discussion started by: khushbu_roy
5 Replies

9. Shell Programming and Scripting

Perl: Filename expansion?

Hi, I do not know if my subject is right, but I at least have your attention :) Recently I posted a perl question related to this. Now when someone starts my program using an asterics: ./dirinfo.pl /orapkg/ora0*/oradata/ It can contain multiple directories. The following command shows... (6 Replies)
Discussion started by: davidg
6 Replies

10. UNIX for Dummies Questions & Answers

Use same filename for prefix in the split command

I want to execute something like this: find . -type f -regex '$REGEX' -print | xargs split -d -C $SIZE The problem is that I want the name of the split files to be the same name as original files. So if my directory has two files called abc.txt and def.txt, then I want the split files to be... (1 Reply)
Discussion started by: namityadav
1 Replies
Login or Register to Ask a Question