Perl script to extract a word from the file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl script to extract a word from the file
# 1  
Old 10-15-2014
Perl script to extract a word from the file

Hi everyone,

I'm a perl newbie and need your help to extract a word inside the list of files with same pattern.

Code:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:mycode xmlns:ns2="http://www.abcd.com/pqrs/acfSchema-2007a.xsd">
    <id>10</id>
    <name>PaymentServices</name>
    <status>active</status>
</ns2:mycode>

Here, i need to extract the word - PaymentServices.
All the XML files will have the same tags and i need to extract the word between the <name> </name> tag.

I think there could be sed or awk code to do this. I need to do this step in the Perl foreach loop over the list of files.
# 2  
Old 10-15-2014
Is this what the XML actually looks like, newlines and all? Or has it been "prettied up" for posting?
# 3  
Old 10-15-2014
Like this ?

Code:
perl -ne 'print if s/<name.*>(.*?)<\/name>/$1/g' file

OR

Code:
perl -F'<|>' -nale  'print $F[2] if $F[1] =~ /name/' file


Last edited by Akshay Hegde; 10-15-2014 at 01:50 PM..
This User Gave Thanks to Akshay Hegde For This Post:
# 4  
Old 10-15-2014
Yes, the XML file will always have same pattern and newlines.

---------- Post updated at 10:15 PM ---------- Previous update was at 10:13 PM ----------

Thanks Akshay, but I need to insert the logic inside the foreach loop (for the list of such files) in a perl code.
# 5  
Old 10-15-2014
Make a loop to read those files line by line, and check them with that statement. It will find the name.
This User Gave Thanks to Corona688 For This Post:
# 6  
Old 10-15-2014
thank you guys.
# 7  
Old 10-15-2014
Try something like this as corona said

Code:
#!/usr/bin/perl -l

use strict;
use warnings;

foreach my $file (@ARGV)
{
	 open (MYFILE, $file) or die "Can't open '$file' for input: $!";
	 while (<MYFILE>) 
	 {
 		chomp;
		my @f=split(/[<>]/,$_);
		print "file : ". $file ."\t".$f[2] if $f[1] =~ /name/
	 }
 	 close (MYFILE);
}

Usage
Code:
$ perl script.pl *.xml

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need to extract the word after a particular keyword throughout the file..

Hi Everyone, Need help in extracting the hostname from the below output. Expected output: DS-TESTB-GDS-1.TEST.ABC.COM DS-TESTB-GDS-2.TEST.ABC.COM .... ... /tmp $ cat -n /tmp/patchreport 1 /usr/bin/perl /admin/bin/patch/applyPatches.pl --apply_patches... (4 Replies)
Discussion started by: thiyagoo
4 Replies

2. Shell Programming and Scripting

Perl script to extract text from image file

Hi Folks, Could you please share your ideas on extracting text from image file(jpg,png and gif formats). Regards, J (1 Reply)
Discussion started by: scriptscript
1 Replies

3. Shell Programming and Scripting

extract a word from text file name

Hi i want to extract the word present before .txt in the text file. For example, Sample_ab_a.txt ----------> i need 'a' Sample_abc_b.txt -----------> i need 'b' Can anyone help me in getting the word extracted (5 Replies)
Discussion started by: Sindhuap
5 Replies

4. Programming

Extract xml data and create word document using perl.

Hi, I have large xml data file.I need to extract node and some tags in the node and after I need to create word document. my XMl data is look like as below -<student> <number>24</number> <education>bachelor</bachelor> <specialization>computers</specialization> ... (3 Replies)
Discussion started by: veerubiji
3 Replies

5. Programming

extract xml data and create word document using perl.

hi, i have large xml file which contains students information, i need to extract student number and some address tags and create a word document for the extracted data. my data looking llike this <student> <number>24</number> <education>bachelors</education> ... (1 Reply)
Discussion started by: veerubiji
1 Replies

6. Shell Programming and Scripting

extract whole thing in word, leaving behind last word. - perl

Hi, i've a string /u/user/DTE/T_LOGS/20110622_011532_TEST_11_HD_120/HD/TESi T_11_HD_120/hd-12 i need to get string, like /u/user/DTE/T_LOGS/20110622_011532_TEST_11_HD_120/HD the words from HD should get deleted, i need only a string till HD, i dont want to use any built in... (4 Replies)
Discussion started by: asak
4 Replies

7. Shell Programming and Scripting

get the fifth line of a text file into a shell script and trim the line to extract a WORD

FOLKS , i have a text file that is generated automatically of an another korn shell script, i want to bring in the fifth line of the text file in to my korn shell script and look for a particular word in the line . Can you all share some thoughts on this one. thanks... Venu (3 Replies)
Discussion started by: venu
3 Replies

8. Shell Programming and Scripting

How to extract just a word from a File in Shell?

Hello Friends, I have a txt file which has data like this TNS Ping Utility for Solaris: Version 10.2.0.3.0 - Production on 23-MAR-2010 15:38:42 Copyright (c) 1997, 2006, Oracle. All rights reserved. Used parameter files: Used TNSNAMES adapter to resolve the alias Attempting to... (7 Replies)
Discussion started by: njafri
7 Replies

9. Shell Programming and Scripting

Perl script to extract 'ID' From XML File

File1.xml <?xml version.........> - <abcd:abcd_list version="1" www.john_uncle's_server.com" xmlns: - <device id="100"> <firmware>12.4(3d)</firmware> <location id="500">Sitting Room</location> </device> - <device id="101"> <firmware>12.4(3d)</firmware> <location id="501">Class... (1 Reply)
Discussion started by: sureshcisco
1 Replies

10. Shell Programming and Scripting

perl newbie: how to extract an unknown word from a string

hi, im quite new to perl regexp. i have a problem where i want to extract a word from a given string. but the word is unknown, only fact is that it appears as the second word in the string. Eg. input string(s) : char var1 = 'A'; int var2 = 10; char *ptr; and what i want to do is... (3 Replies)
Discussion started by: wolwy_pete
3 Replies
Login or Register to Ask a Question