LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 08-10-2007, 12:02 PM   #1
samel_tvom
Member
 
Registered: Aug 2004
Posts: 133

Rep: Reputation: 15
Remove lonely newline


Hi

I would like to remove all lonely newlines from a text file. By that I mean that this text:

Code:
a a a
b b b
c c c

d d d
e e e

f f f
would become
Code:
a a a b b b c c c
d d d e e e
f f f
Any suggestions? I tried an ugly hack with tr and sed:
Code:
cat "$1" |tr "\n" "@"| sed 's/@@/\n/g'|sed 's/@/ /g'
But that really messed up stuff and I'm not surprised.
 
Old 08-10-2007, 01:10 PM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Just an idea in awk:
Code:
/./ { string = string " " $0 }
! /./ { print string ;
        string = "" }
END { print string }
This concatenates non-empty lines (separated by one space) and print them when an empty line is encountered.
 
Old 08-10-2007, 01:10 PM   #3
0.o
Member
 
Registered: May 2004
Location: Raleigh, NC
Distribution: Debian, Solaris, HP-UX, AIX
Posts: 208

Rep: Reputation: 35
Didn't read the whole question.

http://www.student.northpark.edu/pem...d/sed1line.txt

This file still kicks ass.
 
Old 08-10-2007, 02:37 PM   #4
samel_tvom
Member
 
Registered: Aug 2004
Posts: 133

Original Poster
Rep: Reputation: 15
Colucix, yeah, I kinda suck at gawk. How would I implement it? If I have a textfile foo.txt that I would want that awk code to work on, how would I do?
 
Old 08-10-2007, 04:37 PM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
To execute awk code you can put it in a file and execute with the -f option, as in
Code:
gawk -f program.awk foo.txt
or put awk statements directly on the command line, as in
Code:
gawk '{print $1}' foo.txt
You can also have a look at the Gawk: effective AWK Programming guide, a real must-read!
 
Old 08-11-2007, 07:37 AM   #6
samel_tvom
Member
 
Registered: Aug 2004
Posts: 133

Original Poster
Rep: Reputation: 15
Sweet! Thanks a lot!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Sed remove trailing newline nc3b Programming 20 06-26-2015 11:02 AM
Lonely Geek rch General 21 08-13-2003 02:45 AM
FVWM and a lonely gentooist jsrlepage Linux - Software 1 08-08-2003 04:14 PM
What is a newline? raptorsoft2000 Linux - Newbie 6 08-04-2003 10:05 PM
Beautifull BLONDE and LONELY Girl needs help... kabong Linux - Newbie 2 08-18-2001 11:23 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 01:03 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration