Quote:
Originally Posted by corfuitl
hi,
Could you tell me how can I use the sed command in a perl script? Basically, my perl script extracts a variable which is a number of line ($line) of a i file. I want print the same line of j file. In bash the command should be
Code:
sed -n "${line}p" j.txt >> output.txt
|
I'm a bit confused here....if the script already extracts a variable which is a line number, why do you need sed to do anything, since you've already got the variable???
The way I'm reading it, you either get the line number from file "i", which is the $line variable, and want to print the same line number from file "j":
Code:
open (FILEJ, $filename);
@lines = <FILEJ>;
close(FILEJ);
#Get the incoming line from whatever, subtract 1 for the offset.
$line = $line-1;
print $lines[$line];
So you just read the file into an array, and hork out the line you want. Of course, with a huge file that can be problematic.
Or you want to search for a particular pattern in file j which you get from file i, using sed operators in the variable definition:
Code:
my $find = "<some string or regex goes here>";
open FILE, "<filej";
my @line = <FILE>;
print "Lines that match $find\n";
for (@lines) {
if ($_ =~ /$find/) {
print "$_\n";
}
}