We help IT Professionals succeed at work.

Remove blank lines in a text document

vkchaitu82
vkchaitu82 asked
on
Medium Priority
410 Views
Last Modified: 2012-06-27
I have a txt document with many empty lines. How can I remove the empty lines in perl and Vi in linux.
In perl from command line I tried this: perl -ni -e "s/^n//" sample.txt  - this is not working.
Also can any one help me in getting solutoion f this in VI editor and perl from command line.,

2) in Linux in VI how can I get ^M charcter printed so that I can use s/^M// t replace empty liness
Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015
Commented:
perl -i -ne 'print if /\S/' sample.txt

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Maciej Ssysadmin
CERTIFIED EXPERT
Commented:
To get ^M in vi press ctrl-v then ctrl-m (or just hit enter - it's ctrl-m :)).
CERTIFIED EXPERT
Top Expert 2007
Commented:
in vi editor and while in edit mode you can press cntrl-v followed by cntrl-m
cat text.txt|grep -v "^$" >> newtext.txt


this will remove all blank lines
ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
is a line containing only whitespace considered a blank line?
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.