Perl: searching for a string in a file...


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl: searching for a string in a file...
# 1  
Old 09-14-2007
Perl: searching for a string in a file...

Hi All,

I need to search for a string in a file that I've opened and base a decision on the result. The logic is this:

"if the word 'Shared' appears on the first line then

do this on the whole file

else

do this on the whole file

"

The code I currently have isn't working:

Code:
           
if ($. = 1 && $_ =~ /^*Shared*$/) {

             $_ if ($awk - $F) . "\",\"";if ($. != 1) {
             $n = substr($Fld[11], 1, length($Fld[11]) - 2);
             $lines{$. - 1} = $n;

             }
           } else {

             $_ if ($awk - $F) . "\",\"";if ($. != 1) {
             $n = $Fld[10];
             $lines{$. - 1} = $n;

             }
           }

The pattern matching in this code was provided by using a2p (I know awk better than I know perl)...

Thanks in advance.
# 2  
Old 09-14-2007
Hi.

I hope you will have a lot of fun and reward with perl -- I certainly have.

Briefly, regular expressions are not like filename expressions, so "d*" in perl (as in "... shared*") means "0 or more d's", not "d followed by anything".

I suggest you provide:

1) a small sample input that fails, and / or:

2) be more precise by "isn't working" ... cheers, drl

( edit 1: corrected explanation )

Last edited by drl; 09-15-2007 at 02:44 PM..
# 3  
Old 09-17-2007
Hi drl,

Thanks for your reply - I'm certainly finding Perl more productive than using bash shell scripting but it's a little harder to learn - I'm sure I'll get there tho Smilie

The match I'm looking for is actually in the top line of the file and not the filename itself:

Quote:
$ grep Shared MemorySwapUtilisation.csv
"Date","Time","Free Memory KB","Used Memory KB","% Memory Used","Shared Memory KB","Kernel Buffers KB","Data Cache KB","Free Swap Space KB","Used Swap Space KB","% Swap Used"
There are two types of input files (output from sar) because the systems I'm extracting the data from are configured slightly differently. Hence the need to work out which type of file I'm looking at...

So when I've found out whether the work 'Shared' exists in the header line I want to perform one of two possible options on the rest of the file.

Hope this makes my question clearer?

Thanks, p.
# 4  
Old 09-17-2007
Perl: checking the contents of the first line..

Hi All,

I think I've found the solution to my problem. Here's how I found the answer:

1. I knew how to do it in awk:
Code:
$ cat awk1
NR = 1 && $0 ~ /Shared/ { print $0 }

2. So I used a2p to convert it into Perl:
Quote:
$ a2p awk1
#!/usr/bin/perl
eval 'exec /usr/bin/perl -S $0 ${1+"$@"}'
if $running_under_some_shell;
# this emulates #! processing on NIH machines.
# (remove #! line above if indigestible)

eval '$'.$1.'$2;' while $ARGV[0] =~ /^([A-Za-z_0-9]+=)(.*)/ && shift;
# process any FOO=bar switches

$, = ' '; # set output field separator
$\ = "\n"; # set output record separator

while (<>) {
chomp; # strip record separator
if ($. = 1 && $_ =~ /Shared/) {
print $_;
}
}
3. I extracted the only bit that I'm interested in:
Code:
if ($. = 1 && $_ =~ /Shared/)

Basically the logic is matching where line number is equal to one AND where that lines contents contains 'Shared'.

Cheers, p.
# 5  
Old 09-17-2007
Quote:
Originally Posted by pondlife
Hi drl,

Thanks for your reply - I'm certainly finding Perl more productive than using bash shell scripting but it's a little harder to learn - I'm sure I'll get there tho Smilie
you have not tried Python yet. Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Searching a string in a particular file name

Hello, I have a file name like FIRST_DPF_DAILY_CUST_0826152322.txt i need to extract the string after the third "_" underscore upto timestamp ends i.e CUST_0826152322 can anyone help me with the code Thank you! Regards Srikanth Sagi (3 Replies)
Discussion started by: srikanth_sagi
3 Replies

2. Shell Programming and Scripting

Searching a string stored in other file

I need to design a script which can do the following: I have two files abc.txt and constant.hmtl abc.txt contains some 5 string that I need to see if they exist in contants.html. Constants.hmtl is a very large file around 800 lines. I want to search all the strings present in file... (8 Replies)
Discussion started by: hemasid
8 Replies

3. Shell Programming and Scripting

Searching a string in a file using perl

Hi I would like to read a file using perl and search for a string (last entry). Then read that into an array and do further grep File content for ex: comp=a,value=30,runtime=12,type=lic comp=d,value=15,runtime=2,type=lic comp=a,value=90,runtime=43,type=lic... (1 Reply)
Discussion started by: vivek_damodaran
1 Replies

4. Shell Programming and Scripting

searching each file for a string

Hi Guys... I want to search for each file that contains a particular string. e.g find . -print | xargs grep -i string_name Now my issue is the files that I search in are gzipped. Will I be able to find the string, using the above commands, even if the files are gzipped? Please... (2 Replies)
Discussion started by: Phuti
2 Replies

5. Shell Programming and Scripting

Searching a string in a file

Hi, I am new to unix shell scripting. I have a requirement. Could anyone help me writing the script for the same? Here goes the requirement: I have a config file let's say temp.config. Here is the data in the config file temp.config : ------------- name=victor age=42 state=texas... (5 Replies)
Discussion started by: badrimohanty
5 Replies

6. Shell Programming and Scripting

PERL: Searching for a string in a text file problem

Looking for a bit of help. I need to search for a string of words, but unfortunately these words are located on separate lines. for example the text output is: United Chanmpions Ronaldo Liverpool Losers Torres and my script code is print("DEBUG - checking file message"); while... (15 Replies)
Discussion started by: meevagh
15 Replies

7. UNIX for Dummies Questions & Answers

searching for a string in a file

I need to search for a specific string in a file and if this string exist I need to replace it with something else. I am not sure how I could do this, using an if statement. (2 Replies)
Discussion started by: ROOZ
2 Replies

8. Shell Programming and Scripting

Extracting a string from one file and searching the same string in other files

Hi, Need to extract a string from one file and search the same in other files. Ex: I have file1 of hundred lines with no delimiters not even space. I have 3 more files. I should get 1 to 10 characters say substring from each line of file1 and search that string in rest of the files and get... (1 Reply)
Discussion started by: mohancrr
1 Replies

9. Shell Programming and Scripting

Complicated string searching in a file

Hi folks, Following a part of opmn.xml file: <process-type id="OC4J_RiGHTv_PLATOR81" module-id="OC4J"> <environment> <variable id="LD_LIBRARY_PATH" value="/home/ias/v10.1.2/lib" append="true"/> <variable id="SHLIB_PATH"... (5 Replies)
Discussion started by: nir_s
5 Replies

10. UNIX for Dummies Questions & Answers

searching for a string though file system

Is there a way to search an entire file system for the occurance of a string..... other than grep. I have a large directory structure and I'm not certain that grep <string> */*/*/*... is all that effective - especially as I can't be sure of the number of levels to go down without heaps of... (3 Replies)
Discussion started by: peter.herlihy
3 Replies
Login or Register to Ask a Question