Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8169293
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T20:47:59+00:00 2026-06-06T20:47:59+00:00

I am currently facing a problem that seems easy to deal with but I

  • 0

I am currently facing a problem that seems easy to deal with but I haven’t been able to solve it so far. I have a “pipe separated value” file and I would like to remove all leading and trailing spaces on all values on every line. This file may contain several lines.

Example:

1|     | Field2     | Field3
2| 123 | Field2     |
3|     |            | Field3

What I want after removing the spaces:

1||Field2|Field3
2|123|Field2|
3|||Field3

What I have so far works as long as the last field (in this case Field3) is not NULL. This correspond to line 2 on the example above. When the last field is NULL it concatenates that line with the next one creating a bigger line than what is expected.

My code so far is this one:

$res =~ s/\s*\|\s*/\|/g;

As mentioned previously this works as long as the last field on all lines is not NULL. I have tried to come up with a regex that matches all occurrences of a pipe except the last one but without success so far.

Any idea how I could remove all spaces and maintain the line integrity?

Thanks in advance,

João

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T20:48:01+00:00Added an answer on June 6, 2026 at 8:48 pm

    The regex /\s/ matches carriage-returns and newlines (amongst other things) as well as spaces and tabs, so your substitution will delete the trailing newline on records with null final fields.

    Try

    $res =~ s/[ \t]*\|[ \t]*/|/g;
    

    or

    $res =~ s/[ ]*\|[ ]*/|/g;
    

    if you’re not interested in removing tab characters.

    (The pipe in the replacement string doesn’t need escaping.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently facing a problem I haven't been able to solve myself. Basically what
Im currently facing the problem that when i try to set focus on some
I'm currently facing the problem that I want to serve static content via a
I'm developing a Java/Spring web application. The problem I'm currently facing is that I'd
I am currently facing a problem that is concerning the google search, curl and
I've been using Zend framework some time now, but I'm facing a problem I
I'm currently facing new problem with operators. Using following code, I want to make
I'm currently facing a problem which I can not resolve and I really don't
I'm currently facing this problem with ruby on rails 3, devise and facebook oauth.
I'm currently facing a weird problem while executing a command from my bash script.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.