2

I have a perl one-liner:

perl -pe 'while (/((\(.*?[^)]\))|(".*?"))/g) {print "$1 ";} print "\n"' testdata

The file testdata contains the following:

"123" (123) 123

Ouput is:

"123" (123)
"123" (123) 123

But I need:

"123" (123)

What can I do?

A.B.
  • 90,397

2 Answers2

2

How muru commented:

-p prints $_. If you're going to do your own printing, use -n.

perl -ne 'while (/((\(.*?[^)]\))|(".*?"))/g) {print "$1 ";} print "\n"' testdata
Eliah Kagan
  • 117,780
A.B.
  • 90,397
-1

Hm, you could simply put your query (the data-pattern) you are looking for between these symbols and demand strictly this by

'data-pattern'

whatever this data-pattern would contain ... even wild-cards are then allowed too ...

this would look here as part of your intended usage of your command like

'"123" (123)'

or it would look like

\'"123" (123)\'

after your comment I added as follows (please dont downvote me - instead stay constructive with your opinion but do not use your thumbs like in cakepoof) so add or link your perl-line with a pipe to grep -e as follows :

perl -pe 'while (/(((.?[^)]))|(".?"))/g) {print "$1 ";} print "\n"' testdata | grep -e '"123" (123)'

or

perl -pe 'while (/(((.?[^)]))|(".?"))/g) {print "$1 ";} print "\n"' testdata | grep -e \'"123" (123)\'

dschinn1001
  • 3,829
  • 1
    How would that look with this Perl script? perl -pe 'while (/((\(.*?[^)]\))|(".*?"))/g) {print "$1 ";} print "\n"' testdata. The test data can not be changed. – A.B. May 03 '15 at 14:39
  • wait a while ... need a break now ... cause some so-called "gurus" too often deleted postings of me here ... as today ... (clash between money-depending decisions ???) ... – dschinn1001 May 03 '15 at 14:44