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

  • Home
  • SEARCH
  • 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 9296237
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T21:47:22+00:00 2026-06-18T21:47:22+00:00

How can I remove the few lines from a multi-lined variable? I’m getting this

  • 0

How can I remove the few lines from a multi-lined variable?

I’m getting this sort of stuff back from Expect and need to neaten it up a little bit

$var='

total 20
drwxr-xr-x 5 drew.jess users 4096 2013-01-22 15:51 bash
drwxr-xr-x 2 drew.jess users 4096 2011-11-11 10:37 diff
drwxr-xr-x 8 drew.jess users 4096 2012-02-14 09:09 expect
drwxr-xr-x 3 drew.jess users 4096 2011-10-06 11:05 perl 
drwxr-xr-x 3 drew.jess users 4096 2013-02-07 13:10 python
drew ~ $

';

Those blank lines are representative of what I have. Ideally, I want to make $var look like this:

$var='
total 20
drwxr-xr-x 5 drew.jess users 4096 2013-01-22 15:51 bash
drwxr-xr-x 2 drew.jess users 4096 2011-11-11 10:37 diff
drwxr-xr-x 8 drew.jess users 4096 2012-02-14 09:09 expect
drwxr-xr-x 3 drew.jess users 4096 2011-10-06 11:05 perl 
drwxr-xr-x 3 drew.jess users 4096 2013-02-07 13:10 python
';

I should clarify; the important part of this question to me is the removal of the following line:

drew ~ $

The whitespace, I think I can cope with.

  • 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-18T21:47:23+00:00Added an answer on June 18, 2026 at 9:47 pm

    A regular expression like s/^[^\S\n]*\n//gm will remove all lines that contain only whitespace.

    Update

    If you want to do something more than just remove blank lines, then it is best to split the string into lines and remove the ones you don’t want. This code uses grep to remove lines that are all-whitespace and then pop to remove the last one.

    my @lines = grep /\S/, split /\n/, $var;
    pop @lines;
    $var = join '', map "$_\n", @lines;
    print $var;
    

    output

    total 20
    drwxr-xr-x 5 drew.jess users 4096 2013-01-22 15:51 bash
    drwxr-xr-x 2 drew.jess users 4096 2011-11-11 10:37 diff
    drwxr-xr-x 8 drew.jess users 4096 2012-02-14 09:09 expect
    drwxr-xr-x 3 drew.jess users 4096 2011-10-06 11:05 perl 
    drwxr-xr-x 3 drew.jess users 4096 2013-02-07 13:10 python
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

we can remove formula from one cell by cell.setCellFormula(null) . but if i want
How we can remove an array entry from : var a = [],b =
How to I can remove posts from a specific category from homepages in wordpress?
I'm trying to trim a few lines from a file. I know exactly how
Suppose I have a few lines out of wikipedia XML that looks like this:
I have a few cvs files from which I want to read specific lines
One can remove all calls to printf() using #define printf . What if I
Is there a way I can remove the background bar image in the SeekBar
I am looking for an API that can remove bundle items added in a
I am wondering How i can remove a part if my string . For

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.