LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-05-2013, 12:38 PM   #1
corfuitl
Member
 
Registered: Mar 2012
Posts: 38

Rep: Reputation: Disabled
Sed in perl


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
Thanks!
 
Old 08-05-2013, 01:21 PM   #2
codeguy
Member
 
Registered: Jan 2004
Distribution: Slackware
Posts: 187

Rep: Reputation: 46
Not sure if you want a single line (perl golf style) or a script. I'll go script.

Code:
#!/usr/bin/perl

# this is the line to print
my $line = 42;

# we'll read from stdin
while (<>)
{
  if ($. == $line) {
     print;
     last;
  }
}
 
Old 08-05-2013, 01:27 PM   #3
corfuitl
Member
 
Registered: Mar 2012
Posts: 38

Original Poster
Rep: Reputation: Disabled
hi and thanks for your reply.

I want a single line or more but not a script. I am not sure if your lines will work for me since need to import the line from another file.
Thanks!
 
Old 08-05-2013, 01:35 PM   #4
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
codeguy's script for one line could be like this:
Code:
perl -ne 'if ($. == 42) { print; last; }' file
As for your original sed code it's equivalent to this:
Code:
perl -ne "if ($. == ${line}) { print; last; }" j.txt >> output

Last edited by konsolebox; 08-05-2013 at 01:41 PM.
 
Old 08-05-2013, 02:21 PM   #5
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,652

Rep: Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970
Quote:
Originally Posted by corfuitl View Post
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";
    }
}
 
Old 08-05-2013, 02:45 PM   #6
corfuitl
Member
 
Registered: Mar 2012
Posts: 38

Original Poster
Rep: Reputation: Disabled
I think that both work perfectly. I will test them and I will be back soon.
Thank you
 
  


Reply

Tags
perl, sed



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Help on sed and perl needed. cash_05 Programming 7 09-03-2005 04:33 PM
SED, AWK or PERL HELP embsupafly Programming 6 08-20-2005 09:07 PM
using sed with parentheses in perl beebop Linux - General 2 07-21-2005 10:41 PM
SED + perl !!! letmein Linux - General 9 10-07-2003 05:10 PM
perl with sed !!! letmein Linux - General 1 08-18-2003 05:26 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:10 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration