Perl search and append new line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl search and append new line
# 1  
Old 04-18-2011
Perl search and append new line

Dear All,

I want search two lines and append some string in between these lines.

Input file
Code:
tmp,123
,10:123
tmp,666
,50:999
tmp,2:19800
5,3:21.
tmp,2:19800
55555555
tmp,2:19800
5,3:21.

Output should be
Code:
tmp,123
,10:123
tmp,666
,50:999
tmp,2:19800
appending 980000
5,3:21.
tmp,2:19800
55555555
tmp,2:19800
appending 980000
5,3:21.

was trying with following code but not working.
Code:
perl -p0e 's/(,2:19800\n5,3:)1/${1}\nappending 980000/g' input.txt


Thanks in advance

Last edited by radoulov; 04-18-2011 at 12:26 PM.. Reason: Code tags, please!
# 2  
Old 04-18-2011
Try:
Code:
perl -p0e 's/(?<=tmp,2:19800\n)(?=5,3:21.)/appending 980000\n/' input.txt

# 3  
Old 04-19-2011
Thank you very much J could you please explain how it works.
# 4  
Old 04-19-2011
This regular expression uses look-behind and look-ahead to match position in the file between tmp,2:19800 and 5,3:21. Substitution string is then inserted in there.
# 5  
Old 05-19-2011
Need to search in 4 lines and replace with changes in one line and append new line

Search this pattern :

3,71:16/GGGG**111***S***
13,10:51
,487:5/G_CGI
14,9:6/400000


Replace with :

3,71:16/GGGG**111***S***
13,9:6/400000
,10:51
,487:5/G_CGI
14,9:6/400000
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search for a string, then append character to end of that line only

I have 2 files that I am working with $ cat file1 server1 server3 server5 server6 server8 $ cat file2 server1;Solaris; server2; SLES; server3;Linux; server4; Solaris; server5;SLES; server6;SLES; server7;Solaris; server8;Linux; (1 Reply)
Discussion started by: snoman1
1 Replies

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

3. Shell Programming and Scripting

search more than one pattern with perl on same line

Hi friends, I want to search for some hex error codes in some files. After the hex error code is found, the occurences would be counted. Afterwards the found hex errorcode would be cat into a separate file. Here is my code: #!/usr/bin/perl use File::Basename; my $find = $ARGV; my... (2 Replies)
Discussion started by: sdohn
2 Replies

4. Shell Programming and Scripting

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... (3 Replies)
Discussion started by: arvindng
3 Replies

5. Shell Programming and Scripting

how to append a string to next line in perl

hi all , i have a requirement like this.. this just a sample script... $ cat test.sh #!/bin/bash perl -e ' open(IN,"addrss"); open(out,">>addrss"); @newval; while (<IN>) { @col_val=split(/:/); if ($.==1) { for($i=0;$i<=$#col_val;$i++) { ... (2 Replies)
Discussion started by: tprayush
2 Replies

6. Shell Programming and Scripting

Append specific lines to a previous line based on sequential search criteria

I'll try explain this as best I can. Let me know if it is not clear. I have large text files that contain data as such: 143593502 09-08-20 09:02:13 xxxxxxxxxxx xxxxxxxxxxx 09-08-20 09:02:11 N line 1 test line 2 test line 3 test 143593503 09-08-20 09:02:13... (3 Replies)
Discussion started by: jesse
3 Replies

7. Shell Programming and Scripting

search directory-find files-append at end of line

Hi, I have a command "get_data" with some parameters in few *.text files of a directory. I want to first find those files that contain this command and then append the following parameter to the end of the command. example of an entry in the file :- get_data -x -m50 /etc/web/getid this... (1 Reply)
Discussion started by: PrasannaKS
1 Replies

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

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

Using PERL to append chars to each line

I tried using SED to do this, but I'm not having any luck with it. See the previous thread here. I have a program called AMStracker (on OS X) that spits out the values of the motion sensor in the HDD. It has output that looks like this: . . 3 0 -75 3 0 -76 3 0 -77 . . I need to... (5 Replies)
Discussion started by: c0nn0r
5 Replies
Login or Register to Ask a Question