Perl - Inserting text

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl - Inserting text
# 1  
Old 07-22-2009
Perl - Inserting text


I have 10 lines of text ... And I would like to Insert prefix for each line with static text.

perl -pi -e 's/()/$1 TEST$./' data.txt

Each line will have different static prefix, Code above works perfectly for 1st line ... I'm just not sure how I can run same command for 2nd line 3rd etc ....
Thank You in Advance
# 2  
Old 07-22-2009
it should make the change for all the lines in the file. But why is there nothing in between the parenthesis? /()/
# 3  
Old 07-22-2009
Originally Posted by KevinADC
it should make the change for all the lines in the file. But why is there nothing in between the parenthesis? /()/
Hey I guess I need to put (\t\t) for Tabs ?
And again how I can specify that Line2 will have Prefix ABCD Line3 EFG etc ?
perl -pi -e 's/\t/$1 TEST $./' data.txt

Because right now It only adds prefix for 1st line ...

Thank You
# 4  
Old 07-22-2009
this won't do anything

perl -pi -e 's/\t/$1 TEST $./' data.txt

$1 is never defined because there are no capturing parentheses in the search side of the regexp. Maybe this is what you mean:

perl -pi -e 's/(\t)/$1 TEST $./' data.txt

why it does not affect the entire file and only the first line, I don't know.
# 5  
Old 07-22-2009
This code works almost as I would like it .. the only problem is that is not adding text in front of existing line ...

perl -i -ple 'print q{Text to Insert} if $. == 9; close ARGV if eof' data.txt

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with sed and inserting text from another file

I need to insert text from one file into another file after specific term. I guess sed is the best method of doing this and I can insert a specified text string using this script but I am not sure how to modify it to insert text from another file: #!/bin/sh sed 's/\<VirtualHost... (17 Replies)
Discussion started by: barrydocks
17 Replies

2. Shell Programming and Scripting

Inserting some text if a field in the last column changes

Hi, I have a file which looks like this: A 01 00 B 02 00 C 04 00 D 00 01 E 01 01 F 02 01 G 01 04 H 02 04 I want to insert some text if the field if the last column changes. It should look like this: Value 00 A 01 00 B 02 00 C 04 00 Value 01 (6 Replies)
Discussion started by: wenclu
6 Replies

3. Shell Programming and Scripting

Inserting text into a new file

Hi all, I want to create a file and then insert some text into it. I'm trying to create a .sh script that will create a new python file from a template. Can someone tell me why this won't work, touch $1 | sed -e '1i\Some test code here' Sorry I'm quite new to all this! Just as a side... (3 Replies)
Discussion started by: ahodgson
3 Replies

4. UNIX for Dummies Questions & Answers

Inserting a column into a text file

I have a tab delimited text file with multiple columns (data.txt). I would like to insert a column into the text file. The column I want to insert is in a text file (column.txt). I want to insert it into the 5th column of data.txt. How do I go about doing that? Thanks! (2 Replies)
Discussion started by: evelibertine
2 Replies

5. Shell Programming and Scripting

Inserting text with SED

Hi guys, I need to insert after each entry in my .txt file. 1 2 3 4 Tried to use cat test.txt |sed 's/$/'but it does this instead: 1 2 (6 Replies)
Discussion started by: spirm8
6 Replies

6. Shell Programming and Scripting

inserting a string to a text file

Hello Can somebody please help me with the following script? I'm trying to create a text file with 20 blank lines and then insert a string in line 2 but nothing is printed in the itxtfile. I can create the file with 20 blank lines but when I "tell" it to print something on the second line, it... (4 Replies)
Discussion started by: goude
4 Replies

7. Shell Programming and Scripting

Need Help in Inserting a new line in a file using PERL

I need a perl script to find and replace a specific pattern in a file to a new line. BAsically I have a single line data in a file with 10 mb to 200 MB. I want to put a new line based on a specific pattern to open the file in Excel / Access. Following is the sample data in a file ... (1 Reply)
Discussion started by: portalfaq
1 Replies

8. UNIX for Dummies Questions & Answers

inserting a text after a certain word in text files

I need insert a text file content in other text file after certain word like insert content of tagfav.txt in all my html files after the <head> tag. Anyone can help me? (2 Replies)
Discussion started by: ItaloAG
2 Replies

9. Shell Programming and Scripting

Inserting into first record using perl

hi all... i got some requirment where i need to insert some values into first record of flat file. i am using perl for that job. var1=456 var2=789 echo `perl -p -i -e "s/U/\${var1}U${var2}/g;" myFile.txt` but it is writing into all records which has U.... can anyone help me out in this... (7 Replies)
Discussion started by: shreekrishnagd
7 Replies

10. Shell Programming and Scripting

perl/mail - inserting file text in message body

I've got the following code sub mail_report { $Mailer = '/usr/sbin/sendmail'; open MAIL,"|$Mailer"; print MAIL "Subject: $X connection attempt \n"; open MESSAGE, "<$outdir$X"; print MESSAGE '$outdir$X\n'; close MESSAGE; close MAIL; } #End... (2 Replies)
Discussion started by: thumper
2 Replies
Login or Register to Ask a Question