Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > command perl - SR

Reply
Thread Tools

command perl - SR

 
 
fred
Guest
Posts: n/a
 
      09-03-2009
Would like to replace my third field (empty) for each five lines by
foo. This command is not correct. Can you help me fix it ?

!perl -pe 's(&&)($n++ = 5 ? (&foo&)eg' text.txt

xxxx&(ght)(hgf)&&(yyt)
xx9x&(gg)(ff)&&(yyt)
oixxx&(hfd)(jj)&&(yyt)
xxxx&(jj)(kk)&&(yyt)
xjhxxx&(jj)(j)&&(yyt)


Thanks
 
Reply With Quote
 
 
 
 
Tim McDaniel
Guest
Posts: n/a
 
      09-03-2009
In article <(E-Mail Removed)>,
fred <(E-Mail Removed)> wrote:
>Would like to replace my third field (empty) for each five lines by
>foo. This command is not correct. Can you help me fix it ?
>
>!perl -pe 's(&&)($n++ = 5 ? (&foo&)eg' text.txt


I'm afraid that your syntax and semantics have enough wrong that it
would take a while to explain.

I think that approach is barking up the wrong tree anyway. It looks
more like array element manipulation than string processing. I think
it's easier to use split(), either explicitly or implicitly, to split
the input into an array, and then just manipulate $field[2].

You didn't explicitly specify what to do
- if the third field is not empty.
I gather that you would want it to be left alone.
- if there is no third field.
I assume that no third field is to be added.
and the test data didn't include it such test cases.

I also include a test case to make sure that the right number of empty
fields (other than the third) are preserved.

perl '-F&' -wape 'use strict;
if (@F > 2 && $F[2] eq "") {
$F[2] = "foo"; $_ = join("&", @F);
}' 093.txt

applied to

xxxx&(ght)(hgf)&&(yyt)
xx9x&(gg)(ff)&&(yyt)
oixxx&(hfd)(jj)&&(yyt)
xxxx&(jj)(kk)&&(yyt)
xjhxxx&(jj)(j)&&(yyt)
xjhxxx&(jj)(j)&NO FOO HERE&(yyt)
only_one_field
two&fields
&null&&fields&5 more&&&&&

produces

xxxx&(ght)(hgf)&foo&(yyt)
xx9x&(gg)(ff)&foo&(yyt)
oixxx&(hfd)(jj)&foo&(yyt)
xxxx&(jj)(kk)&foo&(yyt)
xjhxxx&(jj)(j)&foo&(yyt)
xjhxxx&(jj)(j)&NO FOO HERE&(yyt)
only_one_field
two&fields
&null&foo&fields&5 more&&&&&


(I don't know a variable to use instead of the repeated "&".)

--
Tim McDaniel, http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
 
sln@netherlands.com
Guest
Posts: n/a
 
      09-03-2009
On Thu, 3 Sep 2009 10:55:18 -0700 (PDT), fred <(E-Mail Removed)> wrote:

>Would like to replace my third field (empty) for each five lines by
>foo. This command is not correct. Can you help me fix it ?
>
>!perl -pe 's(&&)($n++ = 5 ? (&foo&)eg' text.txt
>
>xxxx&(ght)(hgf)&&(yyt)
>xx9x&(gg)(ff)&&(yyt)
>oixxx&(hfd)(jj)&&(yyt)
>xxxx&(jj)(kk)&&(yyt)
>xjhxxx&(jj)(j)&&(yyt)
>
>
>Thanks


perl -pe 's/^(.+&.+&)(&.+)$/$1foo$2/' text.txt
or
perl -pe 's/^((?:.+&){2})(&.+)$/$1foo$2/' text.txt

-sln
 
Reply With Quote
 
sln@netherlands.com
Guest
Posts: n/a
 
      09-03-2009
On Thu, 3 Sep 2009 10:55:18 -0700 (PDT), fred <(E-Mail Removed)> wrote:

>Would like to replace my third field (empty) for each five lines by
>foo. This command is not correct. Can you help me fix it ?
>
>!perl -pe 's(&&)($n++ = 5 ? (&foo&)eg' text.txt
>
>xxxx&(ght)(hgf)&&(yyt)
>xx9x&(gg)(ff)&&(yyt)
>oixxx&(hfd)(jj)&&(yyt)
>xxxx&(jj)(kk)&&(yyt)
>xjhxxx&(jj)(j)&&(yyt)
>
>
>Thanks


If only the first 5 lines:

perl -0 -pe 's/^(.+&.+&)(&.+)/++$n<6 ? $1.foo.$2 : $1.$2/mge' text.txt
or
perl -0 -pe 's/^((?:.+&){2})(&.+)/++$n<6 ? $1.foo.$2 : $1.$2/mge' text.txt

-sln
 
Reply With Quote
 
sln@netherlands.com
Guest
Posts: n/a
 
      09-03-2009
On Thu, 3 Sep 2009 10:55:18 -0700 (PDT), fred <(E-Mail Removed)> wrote:

>Would like to replace my third field (empty) for each five lines by
>foo. This command is not correct. Can you help me fix it ?
>
>!perl -pe 's(&&)($n++ = 5 ? (&foo&)eg' text.txt
>
>xxxx&(ght)(hgf)&&(yyt)
>xx9x&(gg)(ff)&&(yyt)
>oixxx&(hfd)(jj)&&(yyt)
>xxxx&(jj)(kk)&&(yyt)
>xjhxxx&(jj)(j)&&(yyt)
>
>
>Thanks


Or, to fix what you have (untested):

perl -0 -pe 's/(&&)/++$n < 6 ? "&foo&" : $1/eg' text.txt
or
perl -0 -pe 's(&&)(++$n < 6 ? "&foo&" : "&&")eg' text.txt

-sln
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
FAQ 2.17 What is perl.com? Perl Mongers? pm.org? perl.org? cpan.org? PerlFAQ Server Perl Misc 0 02-03-2011 11:00 AM
FAQ 1.4 What are Perl 4, Perl 5, or Perl 6? PerlFAQ Server Perl Misc 0 01-23-2011 05:00 AM
Pull out only first field of ps command ( in Perl ) PERL RUN COMMAND tweetiebirds@gmail.com Perl Misc 2 03-26-2005 06:03 AM
A question about Perl: using perl command line to replace strings... lucy Perl Misc 7 09-03-2004 07:57 AM
Perl Help - Windows Perl script accessing a Unix perl Script dpackwood Perl 3 09-30-2003 02:56 AM



Advertisments