Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: How to substitute all tabs only in a specific field

by kcott (Archbishop)
on May 26, 2020 at 02:39 UTC ( [id://11117257]=note: print w/replies, xml ) Need Help??


in reply to How to substitute all tabs only in a specific field

G'day Xuo,

There are a lot a problems with your post:

  • title has "tabs" but description has "<tabs/space>"
  • data has guillemets but description has "<double quotes>"
  • you're not substituting whitespace just after the opening quote and just before the closing quote (++hippo already commented on this)
  • you've failed to use <code> tags so we can't see the number of whitespace characters: HTML converts strings of spaces and tabs into a single space

Please read "How do I post a question effectively?" carefully.

So, with a lot of guessswork, I believe the following technique, succinctly and efficiently, does what you want:

$ perl -pE 's/^[^"]+"\s+(.+)\s+"/$1 =~ y{ \t}{,}rs/e' a b " x1 x2 " x1,x2 c d " x3 " x3 e f " x4 x5 x6 x7 " x4,x5,x6,x7 x y z " 1space 2spaces spacetabspace end " 1space,2spaces,spacetabspace,end

The 'r' modifier was introduced in Perl v5.14: see "perl5140delta: Non-destructive substitution". Attempting to modify $1 directly results in a "Modification of a read-only value" fatal error. If you have a earlier version of Perl, you'll need to use an interim variable; perhaps something like this:

$ perl -pe 's/^[^"]+"\s+(.+)\s+"/($x = $1) =~ y{ \t}{,}s; $x/e' a b " x1 x2 " x1,x2 c d " x3 " x3 e f " x4 x5 x6 x7 " x4,x5,x6,x7 x y z " 1space 2spaces spacetabspace end " 1space,2spaces,spacetabspace,end

— Ken

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11117257]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2024-04-19 00:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found