perl search and replace - search in first line and replance in 2nd line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting perl search and replace - search in first line and replance in 2nd line
# 1  
Old 02-04-2011
perl search and replace - search in first line and replance in 2nd line

Dear All,

i want to search particular string and want to replance next line value.

following is the test file.
search string is
tmp,???
,10:1 "???" may contain any 3 character it should remain the same and next line replace with ,10:50



tmp,123 --- if match tmp,??? then replace below line
,10:1 --- replace as ,10:50
abc,666
,10:1

tmp,111 --- if match tmp,??? then replace below line
,10:1 --- replace as ,10:50

tmp,123 --- if match tmp,??? then replace below line
,10:1 --- replace as ,10:50
aaa,666
,10:1 -- do not replace as above line is not matching "tmp,???"
,10:1 -- do not replace as above line is not matching "tmp,???"



thank you
# 2  
Old 02-04-2011
Code:
awk '/tmp,[0-9][0-9][0-9]/{print;getline;$0=",10:50"}1' file

# 3  
Old 02-04-2011
Code:
$
$
$ # show the contents of the data file called "f4"
$
$ cat f4
tmp,123 --- if match tmp,??? then replace below line
,10:1 --- replace as ,10:50
abc,666
,10:1
tmp,111 --- if match tmp,??? then replace below line
,10:1 --- replace as ,10:50
tmp,123 --- if match tmp,??? then replace below line
,10:1 --- replace as ,10:50
aaa,666
,10:1 -- do not replace as above line is not matching "tmp,???"
,10:1 -- do not replace as above line is not matching "tmp,???"
$
$
$ # run the Perl one-liner that processes the data in the file "f4"
$
$ perl -plne 'if(/^tmp,\d{3}/) {$x=1} elsif($x) {$_=",10:50"; $x=0}' f4
tmp,123 --- if match tmp,??? then replace below line
,10:50
abc,666
,10:1
tmp,111 --- if match tmp,??? then replace below line
,10:50
tmp,123 --- if match tmp,??? then replace below line
,10:50
aaa,666
,10:1 -- do not replace as above line is not matching "tmp,???"
,10:1 -- do not replace as above line is not matching "tmp,???"
$
$

tyler_durden
# 4  
Old 02-04-2011
Code:
perl -p0e 's/(tmp,...\n,10:)1/${1}50/g' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Multiple line search, replace second line, using awk or sed

All, I appreciate any help you can offer here as this is well beyond my grasp of awk/sed... I have an input file similar to: &LOG &LOG Part: "@DB/TC10000021855/--F" &LOG &LOG &LOG Part: "@DB/TC10000021852/--F" &LOG Cloning_Action: RETAIN &LOG Part: "@DB/TCCP000010713/--A" &LOG &LOG... (5 Replies)
Discussion started by: KarmaPoliceT2
5 Replies

2. Shell Programming and Scripting

Search text and replace line next to it

I have a file which requires modification via a shell script. Need to do the following: 0. take input from user for new text. 1. search for a keyword in the file. 2. replace the line next to this to this keyword with user supplied input. for e.g., my file has the following text: (some... (7 Replies)
Discussion started by: chingupt
7 Replies

3. UNIX for Advanced & Expert Users

Search and replace a line in perl

Hi All, i can replace a perticular value in sentence using perl. perl -pi -e 's/old/new/' sample.txt but i am not able to replace whole string by perl. file1 contains "jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=10.147.109.211)(PORT=1526))(CONNECT_DATA=(SID= MWDBD22)))". i... (3 Replies)
Discussion started by: arindam guha
3 Replies

4. Shell Programming and Scripting

PERL or SHELL Scrript to search in Directories by taking line by line from a text file

Unix box server version *********** >uname -r B.11.00 >echo $SHELL /usr/bin/ksh --> in this server, I have the path like /IMbuild/dev/im0serv1 ---> in that directory I have the folders startup(.jsp files nearly 100 jsp's ) and scripts(contains .js files nearly 100 files) ... (9 Replies)
Discussion started by: pasam
9 Replies

5. UNIX for Advanced & Expert Users

Search Parameter in first line and replace next line content

Hi, I need help. I have XML file as below <a n="infoLevel"> <v s="true"/> </a> <a n="localAddr"> <v s="server.host.com"/> </a> <a n="ListenPort"> <v s="21111"/> </a> I need to find variable "ListenPort" in line and then replace... (4 Replies)
Discussion started by: rdtrivedi
4 Replies

6. Shell Programming and Scripting

Perl script to search a line and copy it to another line

Hi I have a log file (say log.txt). I have to search for a line which has the string ( say ERROR) in the log file and copy 15 lines after this into another file (say error.txt). Can someone give me the code and this has to be in PERL Thanks in advance Ammu (3 Replies)
Discussion started by: ammu
3 Replies

7. Shell Programming and Scripting

search for a string ,replace the whole line with new line

hai i am very new to unix. i am having two files like this. first.properties cache.ZA.TL_CCY=SELECT trim(CCY_CODE)||trim(COUNTRY_CODE)||trim(CITY_CODE) AS... (4 Replies)
Discussion started by: kkraja
4 Replies

8. Shell Programming and Scripting

Perl Search and replace entire line

I have a perl function in my script that needs to replace an entire line in a file sub changestate { my $base = (); my @base = (); open(BASE, $file) || die("Could not open file!"); @base=<BASE>; close (BASE); foreach $base(@base) { if($base =~... (1 Reply)
Discussion started by: insania
1 Replies

9. 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

10. Shell Programming and Scripting

search and replace the whole line

Hi, I have this line in a file called test.cfg SOURCEFILE=/usr/platform/sun4v/driver/file.cfg But i have many occurances of "SOURCEFILE" in test.cfg , i need to search only for this line and replace that line with SOURCEFILE=/usr/platform/sun4x/driver/file.cfg again there are many... (17 Replies)
Discussion started by: Jartan
17 Replies
Login or Register to Ask a Question