PERL, extract value between double quotes


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting PERL, extract value between double quotes
# 1  
Old 06-05-2003
PERL, extract value between double quotes

I know this is probably much simplier than I am making but I need some help please. I have a data file that contains a value on the first line between double quotes ("00043"). I need to assign the value between the first set quotes to a variable in my perl script for comparison analysis. Also, is there anyway to ignore the leading zero's?

For example

"0000006","NEW","1","05152003",.....

$varx = 0000006

Thanks in advance.
# 2  
Old 06-05-2003
Try this:
Code:
yourPerlScript.pl `head -1 dataFile | awk -F"," '{print $1}' | awk -F"\"" '{print $2}'`

Now if that works, then the number should be passed as 0000006 (or 00043 or whatever) to the perl script. I'm not sure how exactly Perl accepts parameters, but I think it's just like a Unix script, so inside your Perl script you'd refer to the number using $1. (So print "$1"; inside your script should display 00043..)

Also, as far as ignoring leading zeroes, Perl should do that automatically depending on how you use the variable.. if you are comparing "0000006" stored in a variable to see if it's less than, say, the number 7, the program should correctly report "TRUE".

Last edited by oombera; 06-05-2003 at 11:09 AM..
# 3  
Old 06-05-2003
This doesn't seem to be picking up the value, perhaps I am not understanding something correctly. Here is what I have regarding your suggestion.

#!/usr/bin/perl


`head -1 /home/crecker/a.dat | awk -F"," '{print $1}' | awk -F"\"" '{print $2}'`;

print "value of var1 \n";
print $1;
print "end of var1 \n";
print "\n";
print "end of script";
print "\n";


The ouput is as follows:

value of var1
end of var1

end of script


It doesn't seem to be picking up $1.

My data set is

"0000006","NEW","1","05152003","USD","04232003","A","","GL","101","097","6106","1211","001","000","5 5","000","0000","200.00","0.00","Test Description 1","000001"
"0000006","NEW","1","05152003","USD","04232003","A","","GL","101","085","0054","1861","000","000","5 6","000","0000","0.00","500.00","Test Description 3","000001"


Thanks again.
# 4  
Old 06-05-2003
Oh, you shouldn't put `head -1 /home/crecker/a.dat | awk -F"," '{print $1}' | awk -F"\"" '{print $2}'`; in the perl script..

You should run the line of code I posted from a unix prompt. Go to a prompt and run yourPerlScript `head -1 /home/crecker/a.dat | awk -F"," '{print $1}' | awk -F"\"" '{print $2}'`
# 5  
Old 06-05-2003
Sorry about that, I can be pretty thick.

Thanks for your help.
# 6  
Old 06-05-2003
If you are attempting to pull the first field from each line within Perl, then you should probably open the file, and grab the variable in the following way (see the man pages for the open, split, and close commands in Perl).

Code:
#!/usr/local/perl
#
$myfile = "./methos";
#
#
        open OLDSHAD, "<$myfile" or die "Can not open methos\n";
        while (<OLDSHAD>) {
                ($usertmp, $everythingelse) = split(/,/, $_, 2);
                system("/usr/bin/echo \"$usertmp\" ");
        }
                close OLDSHAD;

# 7  
Old 06-06-2003
This is a crude example that may do what you want (scriptname.pl)

Code:
#!/usr/bin/perl 

while (<>) {
	if (m/^"([^"]*)",/) {
		$var = sprintf("%d", $1);
		# Replace by some processing here instead of print
		print $var, "\n";
	}
}

Say if your data are in the file "file.dat", type

cat file.dat | perl scriptname.pl

should extract the first field for you and assign to $var, eliminating the leading zeros. You may also want to use open/close to read the data file directly as RTM demonstrated.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extract multiple columns base on double quotes as delimiter

Hi All, I have my data like below "1","abc,db","hac,aron","4","5" Now I need to extract 1,2,4th columns Output should be like "1",abc,db","4" Am trying to use cut command but not able to get the results. Thanks in advance. (4 Replies)
Discussion started by: weknowd
4 Replies

2. Shell Programming and Scripting

Replace Double quotes within double quotes in a column with space while loading a CSV file

Hi All, I'm unable to load the data using sql loader where there are double quotes within the double quotes As these are optionally enclosed by double quotes. Sample Data : "221100",138.00,"D","0019/1477","44012075","49938","49938/15043000","Television - 22" Refurbished - Airwave","Supply... (6 Replies)
Discussion started by: mlavanya
6 Replies

3. Shell Programming and Scripting

Skip the delimiter with in double quotes and count the number of delimiters during data extract

Hi All, I'm stuck-up in finding a way to skip the delimiter which come within double quotes using awk or any other better option. can someone please help me out. Below are the details: Delimited: | Sample data: 742433154|"SYN|THESIS MED CHEM PTY.... (2 Replies)
Discussion started by: BrahmaNaiduA
2 Replies

4. Shell Programming and Scripting

Issue with Single Quotes and Double Quotes for prompt PS1

Hi, Trying to change the prompt. I have the following code. export PS1=' <${USER}@`hostname -s`>$ ' The hostname is not displayed <abc@`hostname -s`>$ uname -a AIX xyz 1 6 00F736154C00 <adcwl4h@`hostname -s`>$ If I use double quotes, then the hostname is printed properly but... (3 Replies)
Discussion started by: bobbygsk
3 Replies

5. Shell Programming and Scripting

Extract data based on 2nd colume having double quotes

i want extract where the 2nd column having "3" or "7". Based on the forums tried like this but it is not working awk -F"," '$2=3;$2=7 {print}' filename Source "1","2","3","4" "1","3","3","4" "1","7","3","4" "1","8","3","4" "1","2","3","4" "1","2","3","4" Output : ... (5 Replies)
Discussion started by: onesuri
5 Replies

6. Shell Programming and Scripting

Preserve commas inside double quotes (perl)

Hi, I have an input file like this $ cat infile hi,i,"am , sam", y hello ,good, morning abcd, " ef, gh " ,ij no, "good,morning", yes, "good , afternoon" from this file I have to split the fields on basis of comma"," however, I the data present inside double qoutes should be treated as... (3 Replies)
Discussion started by: sam05121988
3 Replies

7. Shell Programming and Scripting

How to substitute the value with in double quotes in perl?

Hi, I have string like this: $str=' DNA OR ("rna AND binding AND protein")'; I just wanted to substitute AND with a blank. How can i do that? I want the output like this: $string= DNA OR ("rna binding protein") (3 Replies)
Discussion started by: vanitham
3 Replies

8. Shell Programming and Scripting

Perl echo with double quotes

I need to echo a string that has double quotes in a Perl script. #!/usr/bin/env perl `echo Rule123 -comment \"blah blah\" >> $filename` I'd like to get below appended to $filename: Rule 123 -comment "blah blah" But instead, the double quotes are lost: Rule 123 -comment blah bah ... (1 Reply)
Discussion started by: slchin
1 Replies

9. Shell Programming and Scripting

problems with double quotes in PERL

I have a cgi script I run through apache2 and I need to have a line that contains double quotes within double quotes. Here's what I need PERL to pass to rrdtool: HRULE:30#BBBB00:"30.0 constant":dashesIt's a little more complicated since I also have variables in the statement which requires... (13 Replies)
Discussion started by: audiophile
13 Replies

10. Shell Programming and Scripting

Displaying double quotes using Perl

Hi Guys, I'm a Perl newbie and was wondering if there's a way of displaying double quotes within double quotes. I'm try to print the contents of the variable to a file by using the system function. Here is an example of my code: #============================== $website = <STDIN>;... (2 Replies)
Discussion started by: kbdesouza
2 Replies
Login or Register to Ask a Question