Perl sed question


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl sed question
# 1  
Old 08-22-2012
Perl sed question

Hi,

I'm trying to use perl the way I use sed.

Lets say I have this command:
Code:
$ echo abc | sed 's/abc/&&/'
abcabc

If I try this with perl, I end up with this:
Code:
$ echo abc | perl -pe 's/abc/&&/'
&&

How do I make the '&' sign work?

Another thing that I can't get with perl is this:
Code:
$ echo bazfoobar | sed '/baz/s/foo/bar/g'
bazbarbar

Code:
$ echo bazfoobar | perl -pe '/baz/s/foo/bar/g'
Illegal division by zero at -e line 1, <> line 1.


Last edited by Subbeh; 08-22-2012 at 10:10 AM..
# 2  
Old 08-22-2012
Code:
$ echo abc | perl -pe 's/abc/$&$&/'
abcabc

Edit:
Forget my code on the second example, I got it wrong Smilie

Last edited by zaxxon; 08-22-2012 at 11:07 AM.. Reason: wrong example
This User Gave Thanks to zaxxon For This Post:
# 3  
Old 08-22-2012
Too bad, the second one is very important for me. Thanks for the first one though Smilie
# 4  
Old 08-22-2012
I assume this is the way to go; not sure if there is a shorter sed-stlye way to match first without having to use the if:
Code:
$ echo bazfoobar | perl -pe 'if($_ =~ /baz/){s/foo/bar/g}'
bazbarbar

Check out:
http://perldoc.perl.org/perlretut.html
These 2 Users Gave Thanks to zaxxon For This Post:
# 5  
Old 08-22-2012
Thanks, that's exactly what I was looking for
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. Shell Programming and Scripting

Perl question about

Hello everybody, I am new at the forum and a total newbie when it comes to Unix. I am trying to see how I can add the ability to kill a user's processes? I want to add this to my Shel Script Add the code/process into a subroutine. Also, I would like to use an array to store the list... (0 Replies)
Discussion started by: kinelisch
0 Replies

3. Shell Programming and Scripting

Perl question

Hi All, I am new to Perl and got a real stupid question. We are trying to install the Date:Calc package for some calculations with dates.The security guys mentioned they won't install it as root in /usr/bin/perl but have asked us to install it in any directory and use it from there. Here's the... (2 Replies)
Discussion started by: nua7
2 Replies

4. Shell Programming and Scripting

perl question

Hi guys, I have a file with the following line as data: Does anyone know of a way to assign each number to an array in perl , so each array element would hold single number? I have tried to use "while loop" with a "push", but it puts all line in the first element :( Thanks a lot for... (6 Replies)
Discussion started by: aoussenko
6 Replies

5. Shell Programming and Scripting

Perl question!

Hi All, I am new to perl and just want to read the text file and write back it into another file with some modification. Here is my requirement: input file: USB_OTG_PATH top.usb_top.otg_top USB_HSIC_PATH top.usb_top.hsic_top .. (All starts with USB_) ... START_PATH USB_OTG_PATH.interrupt... (2 Replies)
Discussion started by: surisingh
2 Replies

6. Shell Programming and Scripting

Perl question

Hi I am trying to issue a system call in my Perl script based on the presence of certain pattern in the other file. The pattern is stored in the variable. Can somebody help me with the syntax. This is an example: #!/usr/bin/perl open(MYFILE, "/t1/bin/t1syschk.cmd"); $pattern=cmprsvc; system... (3 Replies)
Discussion started by: aoussenko
3 Replies

7. Shell Programming and Scripting

Question related to sed or awk or perl

Hi All, I have a big file of 100,000 lines with the following format: 1,736870,736870,1,2,5,547,1253889535,1253889775,240,30,152.163.141.8,US,0,00,-g 1253889445 1,881246,881246,1,2,6,402,1253889535,1253889775,240,30,152.163.141.8,US,0,00,-g 1253889445... (4 Replies)
Discussion started by: toms
4 Replies

8. Shell Programming and Scripting

Sed Question 1. (Don't quite know how to use sed! Thanks)

Write a sed script to extract the year, rank, and stock for the most recent 10 years available in the file top10_mktval.csv, and output in the following format: ------------------------------ YEAR |RANK| STOCK ------------------------------ 2007 | 1 | Exxon... (1 Reply)
Discussion started by: beibeiatNY
1 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 question

im trying to retrieve text in between <title> tags of a tagged document, store the text in a temporary file, feed that file into a porter stemmer program, capture the stemmer's output by piping it into an output file, then reconstructing the original tagged document but with the text in... (1 Reply)
Discussion started by: mark_nsx
1 Replies
Login or Register to Ask a Question