Perl inline replace only on 1st line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl inline replace only on 1st line
# 1  
Old 10-04-2012
CPU & Memory Perl inline replace only on 1st line

I have file file_1.out which contains the data below
Code:
<tr> MAIL # 1 TO src_1 </tr> 
<tr><td class="hcol">col_id</td> <td class="hcol">test_dt</td> <td class="hcol">user_type</td> <td class="hcol">ct</td></tr> 
<tr><td class="bcol">1</td> <td class="bcol">2012-09-20</td> <td class="bcol">A</td> <td class="bwcol">15</td></tr> 
<tr><td class="bcol">1</td> <td class="bcol">2012-09-20</td> <td class="bcol">B</td> <td class="bwcol">90</td></tr> 
<tr><td class="bcol">1</td> <td class="bcol">2012-09-20</td> <td class="bcol">C</td> <td class="bcol">6</td></tr> 
<tr><td class="bcol">1</td> <td class="bcol">2012-09-20</td> <td class="bcol">D</td> <td class="bcol">4</td></tr>

How can I replace only the first line with the following:

Code:
<tr><td colspan="4" class="hcol"> MAIL # 1 TO src_1 </td></tr> 
<tr><td class="hcol">col_id</td> <td class="hcol">test_dt</td> <td class="hcol">user_type</td> <td class="hcol">ct</td></tr> 
<tr><td class="bcol">1</td> <td class="bcol">2012-09-20</td> <td class="bcol">A</td> <td class="bwcol">15</td></tr> 
<tr><td class="bcol">1</td> <td class="bcol">2012-09-20</td> <td class="bcol">B</td> <td class="bwcol">90</td></tr> 
<tr><td class="bcol">1</td> <td class="bcol">2012-09-20</td> <td class="bcol">C</td> <td class="bcol">6</td></tr> 
<tr><td class="bcol">1</td> <td class="bcol">2012-09-20</td> <td class="bcol">D</td> <td class="bcol">4</td></tr>

For now I am replaciong inline:
Code:
find file_1.out -type f | xargs perl -pi -e 's/<tr> MAIL #/<tr><td colspan="4" class="hcol"> MAIL #/gi; s/ <\/tr>/<\/td><\/tr>/gi'

But I dont want to use the MAIL tag in the command, if it appears agian some where in the file it does'nt work.

Thanks for your help.

Last edited by Scott; 10-04-2012 at 02:30 PM.. Reason: Please use code tags for code.
# 2  
Old 10-04-2012
This should replace only on first line in each file:
Code:
find file_1.out -type f | xargs perl  -pi -e '$.==1&&s/<tr> MAIL #/<tr><td colspan="4"  class="hcol"> MAIL #/gi; s/ <\/tr>/<\/td><\/tr>/gi'

This User Gave Thanks to bartus11 For This Post:
# 3  
Old 10-04-2012
Quote:
Originally Posted by bartus11
This should replace only on first line in each file:
Code:
find file_1.out -type f | xargs perl  -pi -e '$.==1&&s/<tr> MAIL #/<tr><td colspan="4"  class="hcol"> MAIL #/gi; s/ <\/tr>/<\/td><\/tr>/gi'

Similarly how can we select only the last line?
# 4  
Old 10-04-2012
It requires some changes to the way you edit the file (you have to load whole file as one line):
Code:
find file_1.out -type f | xargs perl -i -p0e 's/<tr> MAIL #(.*)$/<tr><td colspan="4" class="hcol"> MAIL #$1/gi; s/ <\/tr>/<\/td><\/tr>/gi'

This User Gave Thanks to bartus11 For This Post:
# 5  
Old 10-05-2012
Or:
Code:
| xargs perl -i -pe 'if(eof){s/...........'

Or you may use sed too:
For substituting in first line: sed -i '1s///g'

For substituting in last line: sed -i '$s///g'
This User Gave Thanks to balajesuri For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

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

2. Emergency UNIX and Linux Support

Grep inline use perl

Hi, i have input like this : SS-ID VLAN MAC TIME IP RSSI MODE UAPSD BW GI WMOS DHCP IDENTITY ----- ---- --- ---- -- ---- ---- ----- -- -- ---- ---- -------- 1-1 0 C4:46:19:75:C1:55 23m 192.168.5.253 ... (5 Replies)
Discussion started by: justbow
5 Replies

3. Programming

Perl : Inline program to determine file size

Hi, I have 5 files as below $ ll sam* -rw-rw-rw- 1 sam ugroup 0 Mar 21 06:06 sam3 -rw-rw-rw- 1 sam ugroup 0 Apr 3 22:41 sam2 -rw-rw-rw- 1 sam ugroup 17335 Apr 10 06:07 sam1 -rw-rw-rw- 1 sam ugroup 5 Apr 10 07:53 sam5 -rw-rw-rw- 1 sam ugroup 661 Apr 10 08:16 sam4 I want to list out... (4 Replies)
Discussion started by: sam05121988
4 Replies

4. Shell Programming and Scripting

Perl: Conditional replace based on previous and current value in a line

I need to read the contents of a file. Then I need to grep for a keyword and replace part of the grepped line based on the condition of previous and present line. Example input file: V { port1 = P; port2 = 0; shift_port = P0; /* if next shift_port is P0 I need... (9 Replies)
Discussion started by: naveen@
9 Replies

5. Shell Programming and Scripting

perl: replace multiple word on a line

Hi All, If I have a line as following: ( MA "vertical" ) How can I convert it to as below: ( BC "horizontal" ) Thanks, --Michael (6 Replies)
Discussion started by: mxn731
6 Replies

6. UNIX for Dummies Questions & Answers

Replace line via perl script of file

Hi All, I wanted to do following. 1) If file exit then open file for reading and check if my string is there then i wanted to replace the entire line with that string + something else then close the file. 2) If file does not exit then i need to open the file and write to it. I am done with... (0 Replies)
Discussion started by: Tarun24
0 Replies

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

8. Shell Programming and Scripting

Inline searc and replace inside file

Hello, I have a text file that i want to redirect into a new file , searching and replacing certain string during the opertaion. This should be done using shell script , so it should not be interactive. The script should get four parameters : source file target file source string target... (1 Reply)
Discussion started by: yoavbe
1 Replies

9. Shell Programming and Scripting

replace only 1st word of a line if it comes in the subsequent lines at same postion.

I have a file like this.. Maharastra Mumbai worli Maharastra Mumbai navy maharatra Pune Maharastra Nagpur Karnataka Bangalore Karnataka Mysore Karnataka Mangalore Punjab Amritsar punjab Jalandar my expected outcome should be like this Maharastra Mumbai worli ---------- ... (9 Replies)
Discussion started by: geeko
9 Replies

10. 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
Login or Register to Ask a Question