Problem with sed in perl!!


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem with sed in perl!!
# 1  
Old 08-18-2010
Problem with sed in perl!!

Hi,
I am facing an issue with sed in perl. I have a file which has 2 header lines and one trailer line. I need to process the file without these headers and trailer. My file looks like :
File.txt:-
Code:
Header1
Header2
data1
data2
trailer

For removing header and trailer from file I am using the following command in perl file:
Code:
system (`sed '1d;2d;$d' File.txt > NewFile.txt`);

The problem is, when I am excuting this perl script it is just removing the Headers only and not the trailer from the file.
But when I am directly running this command on command line:
Code:
sed '1d;2d;$d' File.txt > NewFile.txt

It is running fine and is removing both headers and trailer from the file. Not sure, why the same command is working differently in perl??
Any help would be highly appreciated.
Thanks..


Moderator's Comments:
Mod Comment Please use code tags, thank you

Last edited by Franklin52; 08-18-2010 at 02:28 PM..
# 2  
Old 08-18-2010
Quote:
Originally Posted by abhisharma23
...
I am facing an issue with sed in perl.
...
For removing header and trailer from file I am using the following command in perl file:
Code:
system (`sed '1d;2d;$d' File.txt > NewFile.txt`);

The problem is...
The problem is that you are trying to use sed in a Perl program.
That's like clubbing someone to death with a loaded Uzi, as Larry Wall says.

Perl is a full-fledged scripting language and I'd think it could do everything that sed could do. Maybe better.

Code:
$
$
$ cat f9
Header1
Header2
data1
data2
trailer
$
$
$
$ perl -lne 'print "DATA => $_" if $. > 2 && !eof' f9
DATA => data1
DATA => data2
$
$

tyler_durden
# 3  
Old 08-19-2010
Thanks for the reply!!
You are right!! but can anybody please let me know how to use this command in a perl file? If i am copying this command directly in perl file, it is not running and throwing out errors.
I need to redirect the output of this command to a file which will only contain data and no header and trailer information.

Thanks,
# 4  
Old 08-19-2010
Tyler is completely right but if u want to go on that way check your syntax:
Code:
system('sed \'1d;2d;$d\' File.txt > NewFile.txt');

# 5  
Old 08-19-2010
Yeah.. it worked now..

I tried below command:
system(`sed '1d;2d;\$d' File.txt > NewFile.txt`);

Thanks everybody
# 6  
Old 08-19-2010
It is strange that, why do you have both 'backtick' and system... both are for execution of shell command, and you have to use one.

Whats the reason behind that ?

And as your backtick does the variable interpolation, you got the $d not working, and \$d to work..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Rewrite sed to perl or run sed in perl

I am having trouble re-writing this sed code sed -nr 's/.*del(+)ins(+).*NC_0{4}(+).*g\.(+)_(+).*/\3\t\4\t\5\t\1\t\2/p' C:/Users/cmccabe/Desktop/Python27/out_position.txt > C:/Users/cmccabe/Desktop/Python27/out_parse.txt in perl Basically, what the code does is parse text from two fields... (12 Replies)
Discussion started by: cmccabe
12 Replies

2. UNIX for Dummies Questions & Answers

sed Or Grep Problem OR Terminal Problem?

I don't know if you guys get this problem sometimes at Terminal but I had been having this problem since yesterday :( Maybe I overdid the Terminal. Even the codes that used to work doesn't work anymore. Here is what 's happening: * I wanted to remove lines containing digits so I used this... (25 Replies)
Discussion started by: Nexeu
25 Replies

3. Shell Programming and Scripting

Perl/sed Escape Syntax Problem . . .

Greetings! I've run into this before; and am having a spot of trouble trying to figure out the way that Perl would prefer the following example syntax to be formed:#!/usr/bin/perl use strict; use warnings; use diagnostics; `sed -i 's/Hi Mom!\|Hi Dad!/Bye Everyone!/I' ./test.txt`;Perl... (5 Replies)
Discussion started by: LinQ
5 Replies

4. Web Development

problem with exporting vairable from one perl cgi to another perl cgi script while redirecting.

Can anyone tell me how to export a variable from one perl CGI script to another perl cgi script when using a redirect. Upon running the login.pl the user is prompted to enter user name and password. Upon entering the correct credentials (admin/admin) the user is redirected to welcome page. My... (3 Replies)
Discussion started by: Arun_Linux
3 Replies

5. Shell Programming and Scripting

re-Substitution Sed (or Perl)

I have a large text csv file that I'm working with. It will look something like this: D,",E",C O,"F,",I O,gh,R The second column always has a two digit random code (can be numbers, letters or any characters). When one of the characters happens to be a comma, the string is quoted. I want to... (5 Replies)
Discussion started by: beenny
5 Replies

6. Shell Programming and Scripting

Need Help with sed/perl !

In a file the content is 13 box google unix.com "he is google" hello "he is unix.com" - I need to replace each space char with "a" char but not inside the double quoted strings. So, the output must look like, 13aboxagoogleaunix.coma"he is google"ahelloa"he is unix.com"a- I tried with ... (9 Replies)
Discussion started by: gameboy87
9 Replies

7. Shell Programming and Scripting

Sed/Perl help

Some text is like this.... <table>This is first text.</table>mouse <table>This is second text</table>keyboard <table>This is third text</table>Pad I need to insert <a></a> between "mouse","keyboard","Pad". I it possible to do with sed/Perl ? Please help.. The text should look like... (8 Replies)
Discussion started by: gameboy87
8 Replies

8. Shell Programming and Scripting

Convert sed to perl

Can anyone convert this from sed to perl: sed -n '/\var\/log/p' /etc/syslog.conf I think Ive looked at this to much....urgh.. Thanks Ben (5 Replies)
Discussion started by: bigben1220
5 Replies

9. Shell Programming and Scripting

[Perl] Accessing array elements within a sed command in Perl script

I am trying to use a script to replace the header of each file, whose filename are stored within the array $test, using the sed command within a Perl script as follows: $count = 0; while ( $count < $#test ) { `sed -e 's/BIOGRF 321/BIOGRF 332/g' ${test} > 0`; `cat 0 >... (2 Replies)
Discussion started by: userix
2 Replies

10. Shell Programming and Scripting

Perl/Sed script help

Hi All, I would need to generate Oracle Inster scripts from an excel formatted spreadsheet as follows: This needs to be stripped as follows: REC 1, REC 2 etc are the separators of the records ... I beleive a pearl script can be written for this ... anything useful will be of... (8 Replies)
Discussion started by: sabyasm
8 Replies
Login or Register to Ask a Question