Find String in Perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find String in Perl
# 8  
Old 04-25-2008
Quote:
Originally Posted by era
The other mistake is that the table name should be replaced elsewhere on that line, too (and maybe elsewhere, too; mahalakshmi was not very clear on the broader context of this).
Good point.
# 9  
Old 04-25-2008
Replacing numbers in text editor

Hi!

i dont know if question like this was already on forum, and if it was i couldn't find it.
Does anyone have script for changing numbers. For example in one text editor i have:

1,1,2,100
2,2,4,100
3,3,6,100
4,4,500,5000
5,5,600,5000
6,6,700,5000

and it need to be changed in:

1,1,5,100
2,2,10,100
3,3,15,100
4,4,1000,5000
5,5,2000,5000
6,6,3000,5000

and i want that i have 100 rows based on first three rows and 100 rows based on other three ones.

Thanks
# 10  
Old 04-25-2008
spuzh,

Please don't hijack another posters thread, but start a new thread for your question.

Regards
# 11  
Old 04-26-2008
Kevin, Era... You're right! Sorry... But not

SQLstr =~ s/(FROM | from)... either as that would capture that part of the match which is unnecessary... The case insensitive version looks the most elegant (i.e., /from\s(\w+)\./iSmilie, and yes I forgot that the /e (evaluate) at the end works with the substitution, not the pattern Smilie

Also yes, in the real world, any of the SQL statement could be on one or many lines.... I don't know if using "s" following the substitution (which allows "." to match newlines -- s/pattern/substitution/s) would help, or perhaps you can't really do this with a simple pattern sub...

Been playing with some of this a lot lately... Seem to have time on my hands and that's both good and bad.... Smilie
# 12  
Old 04-26-2008
The capturing is generally not critical, but sometimes it is, but you can use extended syntax to avoid the capturing:

SQLstr =~ s/(?:FROM | from)

I think ?: is correct, tells perl not to capture the pattern into memory if it is inside parenthesis.


And yes, in the real world things can get sloppy. A simple regexp may not work in many situations.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

find and Replace String in Perl - Regexp

Trying to find and replace one string with another string in a file #!/usr/bin/perl $csd_table_path = "/file.ntab"; $find_str = '--bundle_type=021'; $repl_str = '--bundle_type=021 --target=/dev/disk1s2'; if( system("/usr/bin/perl -p -i -e 's/$find_str/$repl_str/' $csd_table_path")... (2 Replies)
Discussion started by: cillmor
2 Replies

2. Shell Programming and Scripting

Perl script to read string from file#1 and find/replace in file#2

Hello Forum. I have a file called abc.sed with the following commands; s/1/one/g s/2/two/g ... I also have a second file called abc.dat and would like to substitute all occurrences of "1 with one", "2 with two", etc and create a new file called abc_new.dat sed -f abc.sed abc.dat >... (10 Replies)
Discussion started by: pchang
10 Replies

3. Shell Programming and Scripting

Find string in file and find the all records by string

Hello I would like to get know how to do this: I got a big file (about 1GB) and I need to find a string (for instance by grep ) and then find all records in this file based on a string. Thanks for advice. Martin (12 Replies)
Discussion started by: mape
12 Replies

4. Shell Programming and Scripting

[Need help] perl script to find the occurance of string from a text file

I have two files 1. input.txt 2. keyword.txt input.txt has contents like .src_ref 0 "call.s" 24 first 0x000000 0x5a80 0x0060 BRA.l 0x60 .src_ref 0 "call.s" 30 first 0x000002 0x1bc5 RETI .src_ref 0 "call.s" 31 first 0x000003 0x6840 ... (2 Replies)
Discussion started by: acdc
2 Replies

5. Linux

Find String in FileName and move the String to new File if not found

Hi all, I have a question.. Here is my requirement..I have 500 files in a path say /a/b/c I have some numbers in a file which are comma seperated...and I wanted to check if the numbers are present in the FileName in the path /a/b/c..if the number is there in the file that is fine..but if... (1 Reply)
Discussion started by: us_pokiri
1 Replies

6. Shell Programming and Scripting

How to find the count and replace the particular part of string in perl?

Hi, I am taking the current time using localtime function in perl. For example if the time is: #Using localtime $time = "12:3:10"; I have to replace the value 3 (03) i.e second position to be 03. The output should be: 12:03:10 But if the other string for example: $str:... (1 Reply)
Discussion started by: vanitham
1 Replies

7. Shell Programming and Scripting

Perl : Find a string and Print full line

Hi Need a perl script to read lines in a file, scan for a string named "APPLE" and write to different file the only lines containing the matched string. (5 Replies)
Discussion started by: PrasannaKS
5 Replies

8. Shell Programming and Scripting

Find & Replace string in multiple files & folders using perl

find . -type f -name "*.sql" -print|xargs perl -i -pe 's/pattern/replaced/g' this is simple logic to find and replace in multiple files & folders Hope this helps. Thanks Zaheer (0 Replies)
Discussion started by: Zaheer.mic
0 Replies

9. Shell Programming and Scripting

[Perl] Find one string, change another string.

Hi, In principle I am searching for a Perl equivalent for this sed command: sed "/TIM_AM_ARGS=/ s/60/1440/" $EDIT_FILE > $TEMP_FILE cp $TEMP_FILE $EDIT_FILE I was wondering if it needs to be like this, or that there other, shorter, alternatives: open (TIMENVFILE, "<$timenvfile") or die... (5 Replies)
Discussion started by: ejdv
5 Replies

10. UNIX for Dummies Questions & Answers

Read a string with leading spaces and find the length of the string

HI In my script, i am reading the input from the user and want to find the length of the string. The input may contain leading spaces. Right now, when leading spaces are there, they are not counted. Kindly help me My script is like below. I am using the ksh. #!/usr/bin/ksh echo... (2 Replies)
Discussion started by: dayamatrix
2 Replies
Login or Register to Ask a Question