Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 400
  • Last Modified:

Remove blank lines in a text document

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
0
vkchaitu82
Asked:
vkchaitu82
4 Solutions
 
ozoCommented:
perl -i -ne 'print if /\S/' sample.txt
0
 
Maciej SsysadminCommented:
To get ^M in vi press ctrl-v then ctrl-m (or just hit enter - it's ctrl-m :)).
0
 
omarfaridCommented:
in vi editor and while in edit mode you can press cntrl-v followed by cntrl-m
0
 
ai_ja_naiCommented:
cat text.txt|grep -v "^$" >> newtext.txt


this will remove all blank lines
0
 
ozoCommented:
is a line containing only whitespace considered a blank line?
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now