add lines in file with perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting add lines in file with perl
# 1  
Old 02-11-2008
add lines in file with perl

How to search string like: a[7:0] and replace to
a[0]
a[1]
a[2]
:
:
a[7]
in a file with perl?

Thanks,
Grace
# 2  
Old 02-11-2008
Code:
perl -pi.bak -e 's/^(.)\[(\d):\d\]/$1[$2]/' file

# 3  
Old 02-12-2008
It doesn't work for me. I try to use perl script to handle each line in file. The code I used:
open(OF, $infile);
while ($line = <OF>)
{
my $w = "(.+?)";
$line =~ m/^$w\[$w:$w\]/;
my $name = $1;
my $high = $2;
my $low = $3;
for (my $i=$low; $i<=$high; $i++)
{
$line = sprintf("%s[%d]\n",$name,$i);
}
}

That only one line be replaced: a[7:0] -> a[7], but I need add more 6 lines for a[0] ~ a[7]. How to do that with perl scripts?
# 4  
Old 02-12-2008
Code:
#!/usr/bin/perl 
my $string = "a[7:0]";
$string =~ /\[(\d):(\d)\]/;
foreach ( $2 .. $1) {
 print "a[" . $_ . "]\n";
}

output:
Code:
# ./test.pl
a[0]
a[1]
a[2]
a[3]
a[4]
a[5]
a[6]
a[7]

# 5  
Old 02-12-2008
It's no problem print these lines in screen, but I still can't put these lines in file. Like my code:
open(OF, $infile);
open(NF, ">$outfile");

while ($line = <OF>) {
if ( $line =~ m/\[/ )
{
my $w = "(.+?)";
$line =~ m/^$w\[$w:$w\]/;
for (my $i=$3; $i<=$2; $i++)
{
$line = sprintf $1."[".$i."]\n";
}
}
print NF $line;

The $line only show the latest value (e.g. a[7]), but can't add more 6 lines in file.
e.g. input file:
i_clk
i_Sdi
i_Csb
i_kp_d[7:0]
o_en_cmos

I want to output file as:
i_clk
i_Sdi
i_Csb
i_kp_d[0]
i_kp_d[1]
i_kp_d[2]
i_kp_d[3]
i_kp_d[4]
i_kp_d[5]
i_kp_d[6]
i_kp_d[7]
o_en_cmos

bur current result as:
i_clk
i_Sdi
i_Csb
i_kp_d[7]
o_en_cmos
# 6  
Old 02-12-2008
I misunderstood your original requirements, this should work:

Code:
open(OF, $infile);
open(NF, ">$outfile");

while ($line = <OF>) {
   if ( $line =~ m/^(\w+)\[(\d)/ ){
      my $foo = $1;
      my $digit = $2;
      for (0..$digit){
         print NF "$foo\[$_\]\n";
      }
   }
   else {
      print NF $line;
   }
}
close OF;
close NF;

# 7  
Old 02-13-2008
It works! Thanks so much.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Classify lines in file using perl

The below perl executes and does classify each of the 3 lines in file.txt. Lines 2 and 3 are correct as they fit the criteria for Rule 2. The problem is that line one should be classified VUS as it does not meet the criteria for Rule 1, so Rule 3 is used. However, currently Rule 2 is changing the... (27 Replies)
Discussion started by: cmccabe
27 Replies

2. Shell Programming and Scripting

Extract some lines from one file and add those lines to current file

hi, i have two files. file1.sh echo "unix" echo "linux" file2.sh echo "unix linux forums" now the output i need is $./file2.sh unix linux forums (3 Replies)
Discussion started by: snreddy_gopu
3 Replies

3. Shell Programming and Scripting

How to delete lines from a file in PERL?

Hi, I have 500 MB of file. I want to retain first line and last line of the file. I am unaware of deleting lines from a file in PERL. How can i do it in PERL? Regards VANITHA (3 Replies)
Discussion started by: vanitham
3 Replies

4. Shell Programming and Scripting

How to get the lines matched of a file in perl?

Hi, I want to match the time in the file and retrieve those contents of the file. I am taking only first two parameters of localtime(time) function minutes and seconds so partial match i am performing. For Example $start = "14:23"; $end = "14:30"; I am matching file contents... (3 Replies)
Discussion started by: vanitham
3 Replies

5. Shell Programming and Scripting

Using Perl to Merge Multiple Lines in a File

I've hunted and hunted but nothing seems to apply to what I need. Any help will be much appreciated! My input file looks like (Unix): marker,allele1,allele2 RS1002244,1,1 RS1002244,1,3 RS1002244,3,3 RS1003719,2,2 RS1003719,2,4 RS1003719,4,4 Most markers are listed 3 times but a few... (2 Replies)
Discussion started by: Peggy White
2 Replies

6. Shell Programming and Scripting

Parsing a file using perl and skipping some lines

Hi, Consider following file with input: `YFLG:NC^Byad_insert constraint {id=600104470} {profile=GENDER == 2} {profile=BEHAVIOR == 17} {profile=SITEATTR_MULT == siteid:211051} {profile=AGE in } yad_insert ad {id=1718286093336959379} {type=R} ^AYFLG:YOO^Byad_insert constraint {id=600104471}... (1 Reply)
Discussion started by: bvids
1 Replies

7. Shell Programming and Scripting

How to remove the specific lines from file using perl

Can anyone tell me what could be the solution to following : I have one .txt file which contains some "seed" information. This seed may appear multiple time in the file so what I want do is if this seed appears again in the file then that line should be removed. Please provide the script code... (4 Replies)
Discussion started by: dipakg
4 Replies

8. Shell Programming and Scripting

How to remove the lines from file using perl

Can anyone tell me what could be the solution to following : I have one .txt file which contains some seed information. This seed may appear multiple time in the file so what I want do is if this seed appears again in the file then that line should be removed. here is the contents of .txt... (5 Replies)
Discussion started by: dipakg
5 Replies

9. Shell Programming and Scripting

PERL: add string to multiple lines

Dear all, I am stuck while trying to add a string to multiple lines. Let me try to explain using an example: Input: -------- myExample_send ("MasterSends", n, "Data Type", MPI_INT); correct Output:... (4 Replies)
Discussion started by: bonny
4 Replies

10. Shell Programming and Scripting

strip first 4 and last 2 lines from a file using perl

Hi I have a file from which i need to remove the first 4 and the last 2 lines.. i know how to do it with sed but i need to do it in a perl script.. can you please help me how to do that. Thanks (10 Replies)
Discussion started by: meghana
10 Replies
Login or Register to Ask a Question