The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. Shell Script Page.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
perl -write values in a file to @array in perl meghana Shell Programming and Scripting 12 6 Days Ago 01:38 PM
[PERL] Running unix commands within Perl Scripts userix Shell Programming and Scripting 1 05-28-2008 03:06 PM
Calling a perl script from a perl script new2ss Shell Programming and Scripting 3 02-06-2007 07:17 PM
Perl: Run perl script in the current process vino Shell Programming and Scripting 10 12-09-2005 06:45 AM
Replace Perl Module name in all Perl scripts rahulrathod Shell Programming and Scripting 2 12-01-2005 09:00 PM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-16-2008
Registered User
 

Join Date: Feb 2008
Posts: 56
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
help with perl

Hi,

I have a set of files where one of the lines in each file will have a <cd> tag in it when i grep for it. It may be in two formats,

1. </ab><cd>35250</cd>
2. <cd>35250</cd>

However it may be, i need to get the number value between <cd>" "</cd>.

Hope i am clear on my question, I am writing a perl script, i have used substr and index commands, but did not work for me, if anyone has any suggestions i appreciate it.

Thanks--
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 05-16-2008
era era is online now
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 2,253
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
Code:
if (m%<cd>(.*?)</cd>%) { $stuff = $1 }
... provided the start and end tags are always on the same line. If there can be multiple occurrences on the same line, you need to add a loop and a /g flag, too.
Reply With Quote
  #3 (permalink)  
Old 05-16-2008
Registered User
 

Join Date: Feb 2008
Posts: 56
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
Smile

Quote:
Originally Posted by era View Post
Code:
if (m%<cd>(.*?)</cd>%) { $stuff = $1 }
... provided the start and end tags are always on the same line. If there can be multiple occurrences on the same line, you need to add a loop and a /g flag, too.
Thanks era, yes they are always in the same line. Can you please explain me what exactly is it doing?
as far as my understanding goes - (.*?) this is fetching the all the values in between the tags .. what does the m% do? and is $1 output from the if condition?
if (m%<cd>(.*?)</cd>%) { $stuff = $1 }

thanks again for you quick reply
Reply With Quote
  #4 (permalink)  
Old 05-16-2008
era era is online now
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 2,253
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
m%....% is a regular expression match; you'd normally see /.../ or m/.../ but we don't want to use slashes when the pattern itself contains slashes.

If there is a match, the part of the string which matched the parenthesized part of the regular expression will be in $1.
Reply With Quote
  #5 (permalink)  
Old 05-16-2008
Registered User
 

Join Date: Feb 2008
Posts: 56
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
Thanks for you reply era. THis is the code i am using to fetch the number value in between <cd>????</cd> .. but this is the case when this tag is in the same way in all files .. but when </ab><cd>????</cd> this is the case where this will not be able to handle ..
i am little confused about how to fit in ur code here .. can you help me on this .. i really appreciate it.. Thanks--

foreach my $file (@array_files){
open(fh,">>","tmp.out");
# if (m%<cd>(.*?)</cd>%) { $stuff = $1 }
print fh `tail $file|grep "^<cd>"|grep "</cd>"| cut -d"<" -f 2|cut -c 4-`;
close fh;
}
Reply With Quote
  #6 (permalink)  
Old 05-16-2008
era era is online now
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 2,253
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
What's the point of using Perl if the one thing Perl can do really well is handled by a shell function?

Code:
#!/usr/bin/perl -n
if (m%<cd>(.*?)</cd>% { print "$1\n" }
Save that to a file and run it from a shell script which invokes it on each file in turn. Or just type this one-liner directly at the prompt.

Code:
perl -ne 'if (m%<cd>(.*?)</cd>%) { print "$1\n" }' *.txt >tmp.out
Reply With Quote
  #7 (permalink)  
Old 05-16-2008
Registered User
 

Join Date: Feb 2008
Posts: 56
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
Thanks era, the one liner helps!
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes


The 50 most popular UNIX and Linux searches.
Google Search Cloud for The UNIX and Linux Forums
421 service not available, remote server has closed connection ^m automate ftp autosys awk trim bash eval bash exec bash for loop command copy/move folder in unix couldn't set locale correctly curses.h cut command in unix export command in unix find grep find mtime find null character in a unix file grep multiple lines grep or grep recursive hp-ux ifconfig inaddr_any inappropriate ioctl for device lynx javascript mailx attachment mget mtime ping port remove first character from string in k shell replace space by comma , perl script scp recursive segmentation fault(coredump) sftp script snoop unix solaris change ip address stale nfs file handle syn_sent tar exclude tar extract to folder test: argument expected unix unix .profile unix forum unix forums unix internals unix interview questions unix mtime unix simulator unix.com vi substitute while loop within while loop shell script


All times are GMT -7. The time now is 04:00 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101