Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 03-21-2007, 04:22 AM   #1
LQ Newbie
Registered: Mar 2007
Posts: 9

Rep: Reputation: 0
Question Removing new lines from a file

Can any body tell me whether there is any command in bash shell that removes new line(i.e"\n") from a file
Old 03-21-2007, 04:25 AM   #2
Senior Member
Registered: Nov 2003
Location: Perth, Western Australia
Distribution: Ubuntu, Debian, Various using VMWare
Posts: 2,088

Rep: Reputation: 62
You need to give us more information if you want a decent response.

This can be done with tools like sed, or you could write a script in perl.

I hope this isn't homework

Old 03-21-2007, 04:48 AM   #3
Registered: Dec 2006
Location: Bracknell, UK
Distribution: SUSE 13.1
Posts: 159

Rep: Reputation: 33
This thread might help:
Old 03-21-2007, 11:28 PM   #4
Registered: Jun 2006
Distribution: Ubuntu 7.04 Feisty
Posts: 79

Rep: Reputation: 15
sed -n 'H;${g;s/\n/ /g;p}'

Last edited by varun_shrivastava; 03-21-2007 at 11:30 PM.
Old 03-22-2007, 12:50 PM   #5
Registered: Dec 2003
Location: Toronto, Canada
Distribution: Mint, Mandriva
Posts: 221

Rep: Reputation: 31
Originally Posted by psandeepnair1985
Can any body tell me whether there is any command in bash shell that removes new line(i.e"\n") from a file
tr -d '\012' < FILE
If you want a purely bash solution:

file=$( < FILE )
printf "%s" "${file//"$NL"/}"
Old 03-25-2007, 11:46 AM   #6
LQ Guru
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 8,455
Blog Entries: 4

Rep: Reputation: 2918Reputation: 2918Reputation: 2918Reputation: 2918Reputation: 2918Reputation: 2918Reputation: 2918Reputation: 2918Reputation: 2918Reputation: 2918Reputation: 2918
For this general class of problems, there are three Unix/Linux tools that you need to be aware of.

(a) sed ("stream editor") is a tool that can take an input stream (file...), apply a filter or change to it, and write it to an output stream. This tool is usually used with the "|" (pipe) operator in a command.

(b) grep ("regular expressions") is a great tool for finding files and for extracting useful pieces from those files.

(c) awk is a very powerful tool for ripping a file apart, record-by-record and field-by-field, and doing very sophisticated things with them.

Obviously, above-and-beyond this, programming languages like perl and python feature hundreds of pre-built packages of existing code for doing these things and more.

Important Tip... In the Unix/Linux world, there are many ways to do just about anything, and most if not all of them have already been done. Usually, when you think about "building" a solution, you just need to "find" one.
Originally Posted by a wise and very lazy sage:

Dictum ne agas -- Do not do a thing already done.

Last edited by sundialsvcs; 03-25-2007 at 11:48 AM.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Removing partially unique lines lluciano Programming 9 01-11-2006 11:20 AM
[bash] removing or editing lines in file Erhnam Programming 12 03-15-2005 07:25 AM
Removing duplicate lines with sed tireseas Programming 10 01-12-2005 03:27 AM
removing lines from file script iluvatar Programming 9 08-20-2004 05:49 AM
Removing lines from file Aylar Programming 2 04-22-2004 06:34 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:04 PM.

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