Perl search and replace in range using variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl search and replace in range using variable
# 1  
Old 07-01-2008
Perl search and replace in range using variable

Hi. I have a file with asterisk field separators and backslash line terminators. The first field in each line names the line type. I am trying to process each range separately. Here's what the data looks like:

BA*DATA\
LS*DATA1*DATA2*00020*\
TA*DATA1*DATA2*DATA3*\
TA*DATA1*DATA2*DATA3*\
LE*DATA1*DATA2*00020*\
LS*DATA1*DATA2*00020*\
TA*DATA1*DATA2*DATA3*\
TA*DATA1*DATA2*DATA3*\
LE*DATA1*DATA2*00020*\
BA*DATA\


So what I want do is get the ranges of text between LS and LE. Then with each range I want replace the '00020' occurrences with a number that is increased each iteration. So on the first pass '00020' is changed to '1' then on the next range 00020 is changed to '2'. Here's how the data should look after processing:

BA*DATA\
LS*DATA1*DATA2*1*\
TA*DATA1*DATA2*DATA3*\
TA*DATA1*DATA2*DATA3*\
LE*DATA1*DATA2*1*\
LS*DATA1*DATA2*2*\
TA*DATA1*DATA2*DATA3*\
TA*DATA1*DATA2*DATA3*\
LE*DATA1*DATA2*2*\
BA*DATA\


I can get the ranges with this using sed and awk:
Code:
/^LS\*/,/^LE\*/

Since I want to work with the range of data I can't just process it line by line because I need to update the range of data. Everything seems to treat the ranges as one big range. I am trying to learn perl too so I thought maybe I could use perl but I don't know how to put a multi-lined string into a perl array. If anyone has any suggestions as to what would be the best approach I would be very grateful.

Thank you.
# 2  
Old 07-01-2008
Look at the /m and /s modifier of m// for regular expression (same for s///). They should work for you.

perlre - perldoc.perl.org
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Search and replace with variable

Hello all, I stumbled upon a command line for multiple search and replace within given destination perl -pi -w -e 's/SEARCH_FOR/REPLACE_WITH/g;' *.html I want to replace the following line where the date is the variable, from <div class="meta"> <ul> <li>05.05.2015 with date tags, like... (5 Replies)
Discussion started by: uninuub
5 Replies

2. Shell Programming and Scripting

Search and replace (perl)

How can I achieve this? Perl would be awesome. Input string a_b c //Note there is a blank here Needed Output a_b_c Thanks (4 Replies)
Discussion started by: dragonpoint
4 Replies

3. Shell Programming and Scripting

search replace with loop and variable

Hi, could anyone help me with this, tried several times but still not getting it right or having enough grounding to do it outside of javascript: Using awk or sed or bash: need to go through a text file using a for next loop, replacing substrings in the file that consist of a potentially multi... (3 Replies)
Discussion started by: wind
3 Replies

4. Shell Programming and Scripting

Perl - search and replace a particular field

Hi, I have a file having around 30 records. Each record has 5 fields delimited by PIPE. Few records in the file having Junk characters in the field2 and field4. I found the junk charcter and I tested it and replace the junk with space with the command below perl -i -p -e "s/\x00/ /g"... (1 Reply)
Discussion started by: ramkrix
1 Replies

5. Shell Programming and Scripting

Search and Replace in Perl

I am trying to write a simple perl script to run on a FreeBSD machine. There are alot of posts here, and I have read so many, yet can not get this script to run. #!/usr/bin/perl -e 's/\r\n/~/' infile.txt outfile.txt I am trying to take a windows text file, move it into Unix, run a script on... (1 Reply)
Discussion started by: mach1
1 Replies

6. Shell Programming and Scripting

Perl: Global Search and replace

I have a file where the rows correspond to individuals and the columns are about 106 variables. Each variable is coded as either ACGT, and "missing" is coded as blank. This is a tab delimited file. I'm trying to replace all blanks (" ") with 0. The simple script I have is only replacing some of the... (3 Replies)
Discussion started by: epi8
3 Replies

7. Shell Programming and Scripting

Search and replace in Perl

Hello, I have a Perl script that reads in an Excel spread sheet and formats the values into a text file. I am having trouble with one column that can have numbers or letters. Excel left justifies the values that start with a letter and right justifies the values that contain only a... (2 Replies)
Discussion started by: jyoung
2 Replies

8. Shell Programming and Scripting

Perl: Search for string on line then search and replace text

Hi All, I have a file that I need to be able to find a pattern match on a line, search that line for a text pattern, and replace that text. An example of 4 lines in my file is: 1. MatchText_randomNumberOfText moreData ReplaceMe moreData 2. MatchText_randomNumberOfText moreData moreData... (4 Replies)
Discussion started by: Crypto
4 Replies

9. Shell Programming and Scripting

perl search and replace pairs

Hello all im facing some kind of problem i have this string : functionA() $" "$ functionB("arg1") $" = "$ i will like to replace all the pairs of opening and closing "$" to be something like that functionA() <#" "#> functionB("arg1") <#" = "#> i cant of course do is with simple ... (1 Reply)
Discussion started by: umen
1 Replies

10. Shell Programming and Scripting

search & replace in variable

Can I use search & replace in any variable? Suppose I have one variable named var1 which holds value "abcabc" I need to search 'a' in var1 and want to replace with 'x' like 'xbcxbc'. Is it possible? Can you provide me an example? Malay (3 Replies)
Discussion started by: malaymaru
3 Replies
Login or Register to Ask a Question