substitution using perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting substitution using perl
# 8  
Old 05-13-2010
Quote:
Originally Posted by arun_maffy
...
Input:

1 abcdefghij
2 the quick brown fox abc
3 yet another line
4 abcdef abcdef abcdef
5 the abc quick brown abcd fox abcde

Output

1 abcdefghij
2 the quick brown fox abc.txt
3 yet another line
4 abcdef abcdef abcdef
5 the abc quick brown abcd fox abcde.txt

...
What's the difference between rows (2) and (5) ?

- Row (2) ends with "abc", so you want ".txt" at its end.
- But Row (5) does not end with "abc". So why a ".txt" at its end ?
- Row (5) is similar to Row (4) because neither of them ends with "abc", so why not a ".txt" at the end of Row (4) ?

tyler_durden

Sorry, my bad. I guess you are checking for existence of the word abc anywhere in the line.

Code:
$
$ cat f3
abcdefghij
the quick brown fox abc
yet another line
abcdef abcdef abcdef
the abc quick brown abcd fox abcde
$
$
$ perl -lne 'print /\babc\b/ ? "$_.txt" : $_' f3
abcdefghij
the quick brown fox abc.txt
yet another line
abcdef abcdef abcdef
the abc quick brown abcd fox abcde.txt
$
$

tyler_durden
# 9  
Old 05-14-2010
Tyler,

This is what I expected and thanks a lot for your effort. If possible could you please explain the command.

Arun

Last edited by arun_maffy; 05-14-2010 at 05:22 AM..
# 10  
Old 05-14-2010
Quote:
Originally Posted by arun_maffy
... could you please explain the command.
...
Code:
perl -lne 'print /\babc\b/ ? "$_.txt" : $_' f3

A couple of things that you should know first:

(a)
$_ represents the current line of file f3 that is being processed by the Perl one-liner.

(b)
In regular expression terminology, "\b" is a word boundary. It doesn't represent a character in a string, but a condition at a certain position. It's a zero-width assertion.

(c)
The regular expression "\babc\b" means - a word boundary, followed by literal characters "a", "b", "c", followed by another word boundary. In effect, it matches the word "abc".

(d)
// is Perl's pattern match operator. So, /\babc\b/ means the word "abc" is searched. Searched where ? Searched in the current line $_. If it is not mentioned, Perl assumes that the current line is to be searched.

All the following forms are equivalent:

Code:
/\babc\b/
$_ =~ /\babc\b/
$_ =~ m/\babc\b/

which can be seen here:

Code:
$
$ perl -lne 'print /\babc\b/ ? "$_.txt" : $_' f3
abcdefghij
the quick brown fox abc.txt
yet another line
abcdef abcdef abcdef
the abc quick brown abcd fox abcde.txt
$
$ perl -lne 'print $_ =~ /\babc\b/ ? "$_.txt" : $_' f3
abcdefghij
the quick brown fox abc.txt
yet another line
abcdef abcdef abcdef
the abc quick brown abcd fox abcde.txt
$
$ perl -lne 'print $_ =~ m/\babc\b/ ? "$_.txt" : $_' f3
abcdefghij
the quick brown fox abc.txt
yet another line
abcdef abcdef abcdef
the abc quick brown abcd fox abcde.txt
$

(e)
"?:" is the ternary operator that Perl borrowed from C. It works like an "if-then-else" - if the argument before the ? is true, the argument before the : is returned, otherwise the argument after the : is returned.

So in this one-liner, if /\babc\b/ is true, then "$_.txt" is returned i.e. the current line followed by ".txt" is returned.

If /\babc\b/ is false, then $_ is returned i.e. the current line is returned as it is.

(f)
The "print" function prints the value returned at (e) to the standard output.

So, essentially the Perl one-liner searches for the word "abc" in the current line of file f3. If found, it prints current line followed by ".txt". Otherwise, it simply prints the current line.

It does this for all lines of file f3 due to the "n" command-line option (the "n" in -lne).

The one-liner could be written in a verbose manner thusly -

Code:
$
$ perl -lne 'if ($_ =~ m/\babc\b/) { print "$_.txt" } else { print $_ }' f3
abcdefghij
the quick brown fox abc.txt
yet another line
abcdef abcdef abcdef
the abc quick brown abcd fox abcde.txt
$
$

In fact, you could be more verbose and write a Perl program that uses the "open" and "close" functions and while loop to open, process and close the file.

HTH,
tyler_durden
This User Gave Thanks to durden_tyler For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sed/awk/perl substitution with multiple lines

OSX I have been grinding my teeth on a portion of code. I am building a bash script that edits a html email template. In the template, I have place holders for SED (or whatever program is appropriate) to use as anchors for find and replace, with user defined corresponding html code. The HTML code... (3 Replies)
Discussion started by: sudo
3 Replies

2. Shell Programming and Scripting

Perl one-line substitution syntax

Hi, With the following Perl syntax, how to print the $_ value after the substitution? s/(\s*|\n)//g foreach (<>); If I use the below code, it produces some numeric output print s/(\s*|\n)//g foreach (<>); (2 Replies)
Discussion started by: royalibrahim
2 Replies

3. Shell Programming and Scripting

Perl equivalent substitution

hi Geeks, my input file contains data like => 53 - Deewana Kar Raha Hai.mp3 54 - Hale Dil.mp3 55 - Ishq Sufiyana.mp3 56 - Abhi Kuch Dino Se.mp3 57 - Pee Loon Hoto Ki Sargam.mp3 I had used sed command to remove the prefix from the file name like sed 's/^\ it gives me the perfect... (4 Replies)
Discussion started by: lohith.dutta
4 Replies

4. Shell Programming and Scripting

Perl substitution

Hi, I'm new to Perl, and I want to change a few columns in a file in order to insert them into a database. The input file looks like this: 00001,"01/1234567" ,"Tst2" 00002,"01/4545646" ,"Tst123456" 00003,"01/8979898" ,"" The output should look like this: 01-1234567,00001... (2 Replies)
Discussion started by: Subbeh
2 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

perl substitution

Hello all I have a strings like " Watch news 24x7 "."x-wars is glowing" " Watch news like 24 x 7"."x-mas will be celebrated" " Dimensions of box is 24x23x47 ". I have to remove the x(by) in between the number. If i just replace x, it will also remove all x's from text which i do not want.... (1 Reply)
Discussion started by: vasuarjula
1 Replies

7. Shell Programming and Scripting

string substitution in perl

Hi, I have a template file and want to replace 3 parameters to the values that I want. these values are in a parameter file. Any idea how to do this in perl? the parameter file looks like: host_name = jupiter PORT = 1562 IPADDRESS = 10.1.34.10 the template file has lots of entry.... (1 Reply)
Discussion started by: melanie_pfefer
1 Replies

8. Shell Programming and Scripting

Perl:string substitution Pattern: ='abc...',

Hi friends, I want to substitute "a ='....'," with ":" in everywhere in a string using Perl. Details: ---------- my $str= " c1='fgfasfgasggfgff.,akhkhahha', c2='bbbn', c3='hg5 sh' "; Required o/p: $str= " c1:c2:c3 " I tried as below: $str=~ s/=\'.*\',/:/g ; print "str=... (14 Replies)
Discussion started by: Niroj
14 Replies

9. Shell Programming and Scripting

Multi-line Substitution Gone Awry - perl

I've been working on this all night and finally have to ask for help... and not just from my coffee pot. I need to replace a line of text only when it is proceeded by a line containing only the letter "H" Input: H -2.204711 -0.922090 -0.024814 P 6-311+G(d) **** C 6-311+G(d)... (7 Replies)
Discussion started by: EmperorNorton
7 Replies

10. Shell Programming and Scripting

Perl Substitution

I have lines in a file that look like this: machine: machinea machine: machineb machine: randomwhatevermachine I want to replace the machine lines with: machine: machinec I tried perl -pi -e "s/#machine:\?*/machine: machinec/" filename But this ended up doing this: ... (2 Replies)
Discussion started by: Lindarella
2 Replies
Login or Register to Ask a Question